Menu

Saving attachments with plain-text names

Help
Remi Vieux
2013-01-31
2013-02-05
  • Remi Vieux

    Remi Vieux - 2013-01-31

    Can we configure something so that attachments link to some resources are saved with plain-text names? E.g. at the moment, if I enter a resource and attach a file named "brilliant_article.pdf", it is saved in the attachment folder as "brilliant_article.pdf". At the moment, it is saved as a random name like 15c0715b82...

    Regards,
    Remi

     
  • Mark Grimshaw

    Mark Grimshaw - 2013-01-31

    Hi Remi,

    The 'random' number is actually a hash of the file contents and this allows me to check that the attachment is not already in the folder thus avoiding duplication (and more) and thus saving hard disk space (for those on external hosts where space is limited).

    I assume you would like to be able to match the current file names directly to the PDF names given when viewing resources? Perhaps a plug-in to allow for this would help (including options to delete attachments)?

    Mark

     
  • Remi Vieux

    Remi Vieux - 2013-01-31

    What I would like is to give an FTP access on the "Attachment" folder so that users can also get their articles from the ftp directly, and having proper filenames would help in understanding what are the files there. But I understand the issue of saving disk space. Also, some users might give the same generic name to different files...

     
  • Mark Grimshaw

    Mark Grimshaw - 2013-01-31

    Yes, all sorts of problems whatever the solution. I could place attachments in folders labelled with their ID number for uniqueness and then named filenames can be used but then the folder names would be equally meaningless.

    As only at least registered users can upload attachments, they could go in folders according to the wikindx username. But this would be little use to other users searching for a particular file (is it under mark/, anonUser/ or remi/?) and it would solve the same filename problem.

    Hashing the attachments solves the uniqueness issue. I've had others asking the same question but, in terms of wikindx operation alone, the current system works very well.

    Mark

     
  • Stephan Matthiesen

    It can be quite useful also in other cases where one want to have offline copies. But I don't think Mark should provide for such special cases right now.

    If you have shell access to your server, you can script something. Extract hash and filename from database table WKX_resource_attachments with a simple SQL query, use this to create symlinks in a separate FTP directory that are named like the original files and link to the hashed filenames. Also has the advantage that you can place the FTP directory outside the wikindx tree. You can also extract the dates and use touch to set the dates of the symlinks.

    Perhaps such scripts (if someone writes them) could be shared somehow, perhaps in a "scripts" folder or something.

     

    Last edit: Stephan Matthiesen 2013-01-31
    • Mark Grimshaw

      Mark Grimshaw - 2013-02-01

      A long time since I've done server admin but that sounds to me like it would work. If you or Remi comes up with such a script and are willing to share it, I'll create a downloads/scripts folder for it.

      Mark

       
      • Stephan Matthiesen

        Here's a script. It should be safe as it doesn't actually do anything other than create another temporary script file with lots of symlink commands. So you can inspect that temporary script and then run it if you think it's ok.

        You have to put in your server details and directories at the beginning.

        I tried it on my server and it works.

        It is VERY rough and I didn't do any error checking or handling of spaces in filenames, and if you run the created script again you'll get errors about existing symlinks. The worst that can happen is that you create a few symlinks that don't have the correct filename.

        Hope this helps
        Stephan

        Edit: i can't add attachments for some reason. Here is the content of the script:

         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        #!/bin/sh
        # extracts filename and hashed filenames from wikindxs database
        # and creates script file to make symlinks
        
        # folder where the symbolic links will be created
        ftpdir=/your/ftp/directory
        wikindxattdir=/your/directory/to/wikindx/attachments
        
        # take the following from the wikindx config.php
        # name of the database server
        WIKINDX_DB_HOST="localhost"
        # name of the database which these scripts interface with:
        WIKINDX_DB="wikindx4";
        # username and password required to connect to and open the database:
        WIKINDX_DB_USER="yourname";
        WIKINDX_DB_PASSWORD="yourpasswd";
        # set the WIKINDX table prefix here:
        WIKINDX_DB_TABLEPREFIX='WKX_';
        
        # call mysql, transform the output into ln commands, and save the result in a temporary script.
        mysql --user=${WIKINDX_DB_USER} --password=${WIKINDX_DB_PASSWORD} \
            --default-character-set=utf8 --skip-column-names ${WIKINDX_DB} \
            --execute="SELECT resourceattachmentsHashFilename, resourceattachmentsFileName FROM ${WIKINDX_DB_TABLEPREFIX}resource_attachments;" \
            | awk -v wikindxattdir=$wikindxattdir -v ftpdir=$ftpdir '{print "ln -s "wikindxattdir"/"$1 " " ftpdir"/"$2}'    \
            > tmp_create_wikindx_symlinks.sh
        
        # if you feel confident, uncomment the following
        # # create the symlinks
        # sh tmp_create_wikindx_symlinks.sh
        #
        # # remove the temporary script again
        # rm tmp_create_wikindx_symlinks.sh
        
         

        Last edit: Stephan Matthiesen 2013-02-02
        • Remi Vieux

          Remi Vieux - 2013-02-04

          Good idea! Thx for the script

           
  • Mark Grimshaw

    Mark Grimshaw - 2013-02-02

    Here it is:

     
  • Stephan Matthiesen

    One important warning/disclaimer for my script: Use it with care and only when you can trust the users of your wikinx installation. My script itself should be safe, but a user can inject code that would be copied into the temporary script file (e.g. if they rename an attachment to something like "$(rm -rf /)"), so do check the temporary file before executing it, unless you absolutely trust your users.

     
  • Mark Grimshaw

    Mark Grimshaw - 2013-02-02

    If you can provide a README Stephan, I'll upload this to the files/ section here.

    Mark

     
  • Mark Grimshaw

    Mark Grimshaw - 2013-02-05

    The script has now been uploaded to the scripts/ folder in the SF files download area.

    Mark

     

Log in to post a comment.