FYI,

As an interim solution I have extracted the filename encoding part of
sql.py and created a stand-alone function to generate a filename from
the table, field and filename. I am using this currently unless
someone informs me that it is a bad idea. At this point the only
problem I see is that if sql.py changes, my function may become out of
sync with the sql.py implementation.
Ideally, it would be nice if the fileEncode was a function provided
for use by the framework as well as sql.py.

So here is the code I am using:

import os
import re
import uuid
import base64
def encodeFilename(filename,tablename,fieldname):
    filename = os.path.basename(filename or file.filename)
    m = re.compile('\.(?P<e>\w{1,5})$').search(filename)
    extension = m and m.group('e') or 'txt'
    uuid_key = str(uuid.uuid4()).replace('-', '')[-16:]
    encoded_filename = base64.b16encode(filename).lower()
    newfilename = '%s.%s.%s.%s' % \
        (tablename, fieldname, uuid_key, encoded_filename)
    newfilename = newfilename[:122]+'.'+extension
    return newfilename

Ted


On Aug 30, 11:59 pm, Ted G <tedg...@gmail.com> wrote:
> I'm not sure of the best way to approach this, any suggestions?
>
> I am inserting records into a table that contains an upload field for
> an image (which is stored in the table as a blob). The user will be
> able to later change this image via a form, and I wish to expose the
> field for download, thus the use of the upload field. However, when I
> first insert a new record, I want to store a default image in the
> upload field as the initial insert is not done via a form.
>
> Currently I am opening a default image stored in an application
> directory containing a set of default images and storing that file's
> contents in the blob used for the upload field. I am not sure, however
> what to do about the filename string that is stored in the upload
> field itself (usually generated on form.accepts). Is the function that
> generates this value from the filename exposed in a manner that I can
> use it to manually generate a value outside of a form.accepts? Or does
> it matter what I store in the upload field as a value (eg. Could I use
> tablename.fieldname.<record id>.filename as a unique value (although
> this would be not optimal for the DB as I have to do an insert to get
> the ID and then an update for the generated filename)?
>
> This code represents what I want to do:
>
> # get the default image
> filename = os.path.join(request.folder,'templates','default.png')
> file = open(filename)
> imageBlob = file.read()
> file.close()
> # generate a unique value for the upload field
> uploadName = generateUploadName('profile','file','default.png')
> db.profile.insert(file=uploadName,file_data=imageBlob)
>
> Not sure if generateUploadName() is a call to an already existing
> web2py method or one that I need to create myself, and if I create
> myself, if it needs to be consistent with with what is normally
> generated when using form.accepts.
>
> Suggestions?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to