• Hi,

    I’ve had a look through previous support topics about naming of files, spaces etc and saw that changing the plugin to be able to support arbitary filenames on export would be quite difficult.

    I hope you don’t mind, but I worked out a way to do it. It’s a bit of a hack at the moment, and only works if you don’t enable frefix from what I can see.

    In ewz-webform.php line 633, I changed
    $fp = popen("zip -0 -j -q - $fnames", 'r');
    to
    $fp = popen("/usr/local/bin/createZipForCompetition.sh $fnames", 'r');

    The original zip command is passed a list of image files and the csv – which then get zipped up and downloaded. The replacement command takes that and processes them into a zip in the exact format I need.

    I’ve put the script I created to do the processing at the end (works on RHEL, but nothing Linux variant specific here).

    So – after all that – here’s the request. I don’t want to have to go in and update the EWZ code after each update. Would it be possible to have a configuration option added which would be the full path to a custom script (which takes parameters of all the filenames and the csv). If this is present in the configuration then show an extra button of something like ‘Customised Download’.

    Based on the script below I think that this might allow others to do downloads in the format they need, without relying on changes to the core plugin.

    The plugin does everything we need – but I wanted to make it easier for our competition secretary to download in the exact format for import into our projection software. Thanks for creating such a great plugin!

    Many thanks,
    Will.

    —-

    #!/bin/bash
    
    # Given a list of files, create a zip suitable for download
    # One of the files will be a CSV
    
    TMPDIR=/tmp/CompetitionZIP_$$
    mkdir $TMPDIR
    mkdir $TMPDIR/orig
    mkdir $TMPDIR/out
    
    # store arguments in a special array 
    args=("$@") 
    # get number of elements 
    ELEMENTS=${#args[@]} 
     
    # echo each element in array  
    # for loop 
    for (( i=0;i<$ELEMENTS;i++)); do 
    	# echo "Processing ${args[${i}]}" 
    	# echo cp "${args[${i}]}" $TMPDIR/orig/
    	cp "${args[${i}]}" $TMPDIR/orig/
    done
    
    # We've now copied all the files into the target location
    # Next we need to process the CSV file
    
    LINECOUNT=<code></code>cat $TMPDIR/orig/*.csv | wc -l<code></code>
    # echo "Lines to handle = $LINECOUNT"
    
    for (( i=2;i<=$LINECOUNT;i++)); do
    	IMAGEAUTHOR=<code>cat $TMPDIR/orig/*.csv | sed -n -e 1p -e ${i}p | csvcut -c &quot;Display Name&quot; | tail -n 1 | tr -d '&quot;'</code>
    	IMAGEFILE=<code>cat $TMPDIR/orig/*.csv | sed  -n -e 1p -e ${i}p | csvcut -c &quot;Filename&quot; | tail -n 1 | tr -d '&quot;'</code>
    	IMAGETITLE=<code>cat $TMPDIR/orig/*.csv | sed  -n -e 1p -e ${i}p | csvcut -c &quot;Title&quot; | tail -n 1 | tr -d '&quot;'</code>
    	IMAGETYPE=<code>cat $TMPDIR/orig/*.csv | sed  -n -e 1p -e ${i}p | csvcut -c &quot;Tp&quot; | tail -n 1 | tr -d '&quot;'</code>
    
    	# echo "===== ENTRY $i"
    	# echo "Author     : >$IMAGEAUTHOR<"
    	# echo "Filename   : >$IMAGEFILE<"
    	# echo "Title      : >$IMAGETITLE<"
    	# echo "Type       : >$IMAGETYPE<"
    
    	if [ -z "$IMAGEAUTHOR" ]
    	then
    		# echo "***** NO AUTHOR - giving up"
    		continue
    	fi
    
    	if [ -z "$IMAGEFILE" ]
    	then
    		# echo "***** NO FILE - giving up"
    		continue
    	fi
    
    	if [ -z "$IMAGETITLE" ]
    	then
    		# echo "***** NO TITLE - giving up"
    		continue
    	fi
    
    	if [ -z "$IMAGETYPE" ]
    	then
    		# echo "***** NO TYPE - giving up"
    		continue
    	fi
    
    	# echo mv "$TMPDIR/orig/$IMAGEFILE" "$TMPDIR/out/${IMAGEAUTHOR}-${IMAGETITLE}.jpg"
    	mv "$TMPDIR/orig/$IMAGEFILE" "$TMPDIR/out/${IMAGEAUTHOR}-${IMAGETITLE}.jpg"
    done
    
    zip -q -0 ${TMPDIR}/generatedzip.zip ${TMPDIR}/out/*
    cat ${TMPDIR}/generatedzip.zip
    
    rm -rf ${TMPDIR}
    
    • This topic was modified 4 years, 1 month ago by wildcardinal.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter wildcardinal

    (@wildcardinal)

    Just to mention – I realise that the script is not currently production strength. Entry titles could be set to contain characters that can break the script, do nasty things etc.
    I need to work on protection etc – but not yet got there!

    Plugin Author Josie Stauffer

    (@joanne123)

    Good luck if you can get this working safely!

    It sounds as if you are a good enough coder to be able to write a script that makes the change in ewz-webform.php 🙂 I’d be quite reluctant to encourage non-coders to run bash scripts like this.

    The trouble with relying on external scripts running on the server is that not all hosting companies allow such things. And you could do some real damage to your site if you make a mistake. ( popen itself is not always available – entrywizard checks that it is before using it, and drops back to a slower method if it is not. )

    One approach I have seen work is to write a spreadsheet macro that generates a column containing the batch commands needed to rename the files after downloading. That runs on the user’s local machine, and requires a couple of clicks to generate the column, followed by a copy-paste to the command line.

    But if you want to use this method to include the title in a filename, you still have to clean it up to remove unsafe characters. I’m not sure if that would be possible inside a spreadsheet macro.

    I am currently working on a way to allow any field in the prefix, at least when it is not applied on upload. The resulting filename would be modified to make sure the it is legitimate on all common OS’s. But that would still replace spaces and special characters.

    As Josie says, if you can code like this, then go the lower risk route of writing in Excel VBA and do your post-processing locally.
    One issue I have is that the various clubs use different competitions software, so could there be post-processing with a variable definition of the required output. There are only so many fields which you could want in a file name – Sequence, Title, Photographer, Entrant (may not be the Photographer), Class/Section/Category, Organiser. And, there are only so many ways of separating those fields using keywords or characters. The Entrant may include characters in a Title which are invalid in filenames eg, !, so clean up is required. DiCentra normally requires each Entrant to be submitted in a different folder, which would involve file copying as well as renaming. All in all enough to keep a coder happy and productive.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom export’ is closed to new replies.