/* * [FileSize.java] * * Summary: Inserts size of some file in form xxx bytes, xxxK or xxx MB. * * Copyright: (c) 2006-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2006-01-13 */ package com.mindprod.htmlmacros.macro; import com.mindprod.common18.EIO; import java.io.File; import java.text.DecimalFormat; import static com.mindprod.htmlmacros.macro.Global.configuration; import static java.lang.System.*; /** * Inserts size of some file in form xxx bytes, xxxK or xxx MB. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2006-01-13 * @since 2006-01-13 */ public final class FileSize extends Macro { /** * bytes in a K */ private static final long ONE_K = 1024; /** * bytes in a megabyte */ private static final long ONE_MB = 1024 * 1024; /** * how to use the macro */ private static final String USAGE = "\nFileSize macro needs uPath file name"; /** * no decimal places */ private static final DecimalFormat df0 = new DecimalFormat( "###,##0" ); /** * one decimal place */ private static final DecimalFormat df1 = new DecimalFormat( "###,##0.0" ); /** * Updated macro expansion * * @param file whose size you want, File is relative to webRoot * * @return macro expansion */ private String expand( String file ) { if ( file == null || file.length() == 0 || file.equals( "null" ) ) { err.println( "warning: macro FileSize null in " + EIO.getCanOrAbsPath( fileBeingProcessed ) + "\n" ); return "0 bytes"; } final File f = new File( configuration.getLocalWebrootWithSlashes(), file ); if ( !f.exists() ) { err.println( "warning: macro FileSize, missing file " + EIO.getCanOrAbsPath( f ) + " in " + fileBeingProcessed .getAbsolutePath() + " so cannot expand.\n" ); return "0 bytes"; } long size = f.length(); if ( size < 10 * ONE_K ) { return "" + df0.format( size ) + " bytes"; } else if ( size < 2 * ONE_MB ) { // rounded K return "" + df0.format( ( ( double ) size / ONE_K ) ) + "K"; } else { // rounded MB to one decimal place return "" + df1.format( ( ( double ) size / ONE_MB ) ) + "MB"; } } /** * Generate date from Note filename is relative to webRoot, not current * document * * @param parms LOCAL_WEBROOT_WITH_BACKSLASHES-relative filename, not relative to file being processed. * If you were including, it would be confusing, relative to including or included * document. * @param quiet true if want output suppressed. * @param verbose @return expanded macro text */ public String expandMacro( String[] parms, final boolean quiet, final boolean verbose ) { if ( !quiet ) { out.print( "F" ); } if ( parms.length < 1 ) { throw new IllegalArgumentException( USAGE ); } if ( parms.length > 1 ) { throw new IllegalArgumentException( "Extraneous parm " + parms[ 1 ] + " after " + parms[ 0 ] + "\n" + USAGE ); } return expand( parms[ 0 ] ); } }