Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hey @allythacker – we had the same need. Renaming natively is quite difficult (as @mbsharp says, the official way is to rename once downloaded). I created this which runs on the server and allows the Zip file created to have files within it renamed. There is no way that I would ever expect this to be rolled into the core of EntryWizard, but if you have some willingness to invest a bit of time (and understand a bit of shell scripting and you run on a Unix server) this may be able to do what you are wanting.

    The first bit is a small update to wp-content/plugins/entrywizard/classes/ewz-webform.php to call a new script rather than just zipping the files together. About 1/3 of the way down this file, replace the line

    $fp = popen("zip -0 -j -q - $fnames", 'r');

    with…

    $fp = popen("/usr/local/bin/createZipForCompetition.sh $fnames", 'r');

    So you get this…

            // options for linux "zip" command:
    // -0 = no compression
    // -j = strip path, use only filename
    // -q = no extra output info
    // $fp = popen("zip -0 -j -q - $fnames", 'r'); // see testing notes
    $fp = popen("/usr/local/bin/createZipForCompetition.sh $fnames", 'r');
    if( !$fp ){ // see testing notes
    error_log("EWZ: Zip command failed");
    throw new EWZ_Exception("Zip command failed");
    }

    This means that rather than just creating a zip we are intercepting and calling our own script to so the renaming for us.

    Then create the shell script to do the real work. This takes the image filenames and csv (passed in as arguments – as they would have been passed into the zip command) and then does all the magic. All the fields in the CSV are available (which includes username, category, image name etc). You just need to extract them from the CSV and use them in the rename – follow the pattern in the script!

    You may need to make a few other updates to get the structures/names exactly as you want.

    I have created this as /usr/local/bin/createZipForCompetition.sh

    #!/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 -p $TMPDIR
    mkdir -p $TMPDIR/orig
    mkdir -p $TMPDIR/out
    #mkdir -p $TMPDIR/out/NoType/fullname
    #mkdir -p $TMPDIR/out/NoType/titleonly
    #mkdir -p $TMPDIR/out/PDI/fullname
    #mkdir -p $TMPDIR/out/PDI/titleonly
    #mkdir -p $TMPDIR/out/Print/fullname
    #mkdir -p $TMPDIR/out/Print/titleonly

    CSVFILETITLE=EntryList-TitlesOnly.csv
    CSVFILE=EntryList.csv

    # 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=
    cat $TMPDIR/orig/*.csv | wc -l
    # echo "Lines to handle = $LINECOUNT"

    #Create the data files
    cat $TMPDIR/orig/*.csv | csvcut -c "Display Name" > $TMPDIR/orig/_DISPLAYNAME.TXT
    cat $TMPDIR/orig/*.csv | csvcut -c "Filename" > $TMPDIR/orig/_FILENAME.TXT
    cat $TMPDIR/orig/*.csv | csvcut -c "Title" > $TMPDIR/orig/_TITLE.TXT
    cat $TMPDIR/orig/*.csv | csvcut -c "Tp" > $TMPDIR/orig/_TYPE.TXT

    # cat $TMPDIR/orig/*.csv

    # echo "======"
    # cat $TMPDIR/orig/_TYPE.TXT
    # echo "======"

    HASNOTYPE="0"
    HASPRINT="0"
    HASPDI="0"


    for (( i=2;i<=$LINECOUNT;i++)); do
    IMAGEAUTHOR=cat $TMPDIR/orig/_DISPLAYNAME.TXT | sed -n -e 1p -e ${i}p | tail -n 1 | tr -d "<>:\"/\\|?*_"
    IMAGEFILE=cat $TMPDIR/orig/_FILENAME.TXT | sed -n -e 1p -e ${i}p | tail -n 1
    IMAGETITLE=cat $TMPDIR/orig/_TITLE.TXT | sed -n -e 1p -e ${i}p | tail -n 1 | tr -d "<>:\"/\\|?*_"
    IMAGETYPE=cat $TMPDIR/orig/_TYPE.TXT | sed -n -e 1p -e ${i}p | tail -n 1 | tr -d "<>:\"/\\|?*_"
    RINDEX=echo ${IMAGEAUTHOR}${IMAGETITLE} | md5sum | tr -d "&#091;a-z0]" | cut -c 3-9

    if [ -z "$IMAGETYPE" ]
    then
    IMAGETYPE="NoType"
    HASNOTYPE="1"
    fi

    if [ "$IMAGETYPE" = "P" ]
    then
    IMAGETYPE="Print"
    HASPRINT="1"
    fi

    if [ "$IMAGETYPE" = "D" ]
    then
    IMAGETYPE="PDI"
    HASPDI="1"
    fi



    # 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

    mkdir -p ${TMPDIR}/out/${IMAGETYPE}/fullname
    mkdir -p ${TMPDIR}/out/${IMAGETYPE}/titleonly

    TARGETFULL="${IMAGETITLE}_${IMAGEAUTHOR}.jpg"
    TARGETSHORT="${RINDEX}_${IMAGETITLE}.jpg"

    SOURCEFILE=$(printf '%q' "$IMAGEFILE")

    TARGETFILEFULL=$(printf '%q' "$TARGETFULL")
    MYCMDFULL="cp $TMPDIR/orig/${SOURCEFILE} $TMPDIR/out/${IMAGETYPE}/fullname/${TARGETFILEFULL}"
    eval $MYCMDFULL

    TARGETFILESHORT=$(printf '%q' "$TARGETSHORT")
    MYCMDSHORT="cp $TMPDIR/orig/${SOURCEFILE} $TMPDIR/out/${IMAGETYPE}/titleonly/${TARGETFILESHORT}"
    eval $MYCMDSHORT

    echo ${RINDEX},\"${IMAGEAUTHOR}\",\"${IMAGETITLE}\" >> $TMPDIR/out/${IMAGETYPE}/${CSVFILE}
    echo ${RINDEX},\"${IMAGETITLE}\" >> $TMPDIR/out/${IMAGETYPE}/${CSVFILETITLE}

    done

    # cp $TMPDIR/orig/*.csv ${TMPDIR}/out


    if [ "$HASNOTYPE" = "1" ]
    then
    cd ${TMPDIR}/out/NoType

    cat $TMPDIR/out/NoType/${CSVFILE} | sort --numeric-sort --field-separator=, --key 1 > $TMPDIR/out/NoType/${CSVFILE}.tmp
    cat $TMPDIR/out/NoType/${CSVFILETITLE} | sort --numeric-sort --field-separator=, --key 1 > $TMPDIR/out/NoType/${CSVFILETITLE}.tmp

    echo "Identifier,Author,Title" | cat - $TMPDIR/out/NoType/${CSVFILE}.tmp > $TMPDIR/out/NoType/${CSVFILE}
    echo "Identifier,Title" | cat - $TMPDIR/out/NoType/${CSVFILETITLE}.tmp > $TMPDIR/out/NoType/${CSVFILETITLE}

    rm $TMPDIR/out/NoType/${CSVFILE}.tmp
    rm $TMPDIR/out/NoType/${CSVFILETITLE}.tmp
    fi

    if [ "$HASPRINT" = "1" ]
    then
    cd ${TMPDIR}/out/Print

    cat $TMPDIR/out/Print/${CSVFILE} | sort --numeric-sort --field-separator=, --key 1 > $TMPDIR/out/Print/${CSVFILE}.tmp
    cat $TMPDIR/out/Print/${CSVFILETITLE} | sort --numeric-sort --field-separator=, --key 1 > $TMPDIR/out/Print/${CSVFILETITLE}.tmp

    echo "Identifier,Author,Title" | cat - $TMPDIR/out/Print/${CSVFILE}.tmp > $TMPDIR/out/Print/${CSVFILE}
    echo "Identifier,Title" | cat - $TMPDIR/out/Print/${CSVFILETITLE}.tmp > $TMPDIR/out/Print/${CSVFILETITLE}

    rm $TMPDIR/out/Print/${CSVFILE}.tmp
    rm $TMPDIR/out/Print/${CSVFILETITLE}.tmp
    fi

    if [ "$HASPDI" = "1" ]
    then
    cd ${TMPDIR}/out/PDI

    cat $TMPDIR/out/PDI/${CSVFILE} | sort --numeric-sort --field-separator=, --key 1 > $TMPDIR/out/PDI/${CSVFILE}.tmp
    cat $TMPDIR/out/PDI/${CSVFILETITLE} | sort --numeric-sort --field-separator=, --key 1 > $TMPDIR/out/PDI/${CSVFILETITLE}.tmp

    echo "Identifier,Author,Title" | cat - $TMPDIR/out/PDI/${CSVFILE}.tmp > $TMPDIR/out/PDI/${CSVFILE}
    echo "Identifier,Title" | cat - $TMPDIR/out/PDI/${CSVFILETITLE}.tmp > $TMPDIR/out/PDI/${CSVFILETITLE}

    rm $TMPDIR/out/PDI/${CSVFILE}.tmp
    rm $TMPDIR/out/PDI/${CSVFILETITLE}.tmp
    fi


    cd ${TMPDIR}/out
    zip -r -q -0 ${TMPDIR}/generatedzip.zip *
    cat ${TMPDIR}/generatedzip.zip

    cd /

    rm -rf ${TMPDIR}

    To Mcollen – yes, it’s the same as you are doing but getting it done on the server first. As long as there is a field in the CSV you can use it as part of the filename creation.

    To Ronc – the down side is that when ever Josie puts out an update I have to go back in and make the update. Not great, but this approach does make it easier for us. I did raise thread asking if the command used to create the zip could be configurable, but Josie wasn’t keen (understandably). Thread here.

    I have a work-around for this which does well for us. I’m not sure if you’ll want to dive in to this level though.

    In wp-content/plugins/entrywizard/classes/ewz-webform.php
    Change the line
    $fp = popen("zip -0 -j -q - $fnames", 'r'); // see testing notes
    To
    $fp = popen("/usr/local/bin/createZipForCompetition.sh $fnames", 'r'); // see testing notes

    Then create the file /usr/local/bin/createZipForCompetition.sh as follows

    #!/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
    mkdir $TMPDIR/out/fullname
    mkdir $TMPDIR/out/titleonly
    
    # 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>cat $TMPDIR/orig/*.csv | wc -l</code>
    # echo "Lines to handle = $LINECOUNT"
    
    #Create the data files
    cat $TMPDIR/orig/*.csv | csvcut -c "Display Name" > $TMPDIR/orig/_DISPLAYNAME.TXT
    cat $TMPDIR/orig/*.csv | csvcut -c "Filename" > $TMPDIR/orig/_FILENAME.TXT
    cat $TMPDIR/orig/*.csv | csvcut -c "Title" > $TMPDIR/orig/_TITLE.TXT
    cat $TMPDIR/orig/*.csv | csvcut -c "Tp" > $TMPDIR/orig/_TYPE.TXT
    
    for (( i=2;i<=$LINECOUNT;i++)); do
            IMAGEAUTHOR=<code>cat $TMPDIR/orig/_DISPLAYNAME.TXT | sed -n -e 1p -e ${i}p | tail -n 1 |  tr &quot;<>:\&quot;/\\|?*_&quot; @</code>
            IMAGEFILE=<code>cat $TMPDIR/orig/_FILENAME.TXT | sed  -n -e 1p -e ${i}p | tail -n 1</code>
            IMAGETITLE=<code>cat $TMPDIR/orig/_TITLE.TXT | sed  -n -e 1p -e ${i}p | tail -n 1 | tr &quot;<>:\&quot;/\\|?*_&quot; @</code>
            IMAGETYPE=<code>cat $TMPDIR/orig/_TYPE | sed  -n -e 1p -e ${i}p | tail -n 1 |  tr &quot;<>:\&quot;/\\|?*_&quot; @</code>
            RINDEX=<code>od -A n -t d -N 4 /dev/urandom | tail -c 6</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 - creating a standard name"
                    TARGETFULL="${IMAGETITLE}_${IMAGEAUTHOR}.jpg"
                    TARGETSHORT="${RINDEX}_${IMAGETITLE}.jpg"
            else
                    # echo "***** HAS TYPE - creating an extended name"
                    TARGETFULL="${IMAGETITLE}_${IMAGETYPE}_${IMAGEAUTHOR}.jpg"
                    TARGETSHORT="${RINDEX}_{IMAGETYPE}_${IMAGETITLE}.jpg"
            fi
    
            SOURCEFILE=$(printf '%q' "$IMAGEFILE")
    
            TARGETFILEFULL=$(printf '%q' "$TARGETFULL")
            MYCMDFULL="cp $TMPDIR/orig/${SOURCEFILE} $TMPDIR/out/fullname/${TARGETFILEFULL}"
            eval $MYCMDFULL
    
            TARGETFILESHORT=$(printf '%q' "$TARGETSHORT")
            MYCMDSHORT="cp $TMPDIR/orig/${SOURCEFILE} $TMPDIR/out/titleonly/${TARGETFILESHORT}"
            eval $MYCMDSHORT
    done
    
    cd ${TMPDIR}/out
    
    zip -r -q -0 ${TMPDIR}/generatedzip.zip *
    cat ${TMPDIR}/generatedzip.zip
    
    cd /
    
    rm -rf ${TMPDIR}

    You may need to update to grab the specific columns you want from the CSV file generated for inclusion in the filename.

    We run this on a Linux hosted environment and not hit any problems yet. We strip out the problematic characters from the titles before renaming the file (tell people that they can’t have things like * < > | etc).

    Works for us, but we need to remember to re-modify the plugin code every time we update.

    Forum: Plugins
    In reply to: [EntryWizard] Custom export
    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!

Viewing 4 replies - 1 through 4 (of 4 total)