I need to upload a file to uploads, process it, and store the
processed result in a new folder/file.
# I've defined the following two models
db.define_table('infile',
Field('parent',db.parent),
Field('name','upload',autodelete=True,uploadseparate=True))
db.define_table('outfile',
Field('parent',db.parent),
Field('name','upload',autodelete=True,uploadseparate=True))
# Here's the controller for uploading the input file
def input():
... blah ...
if form.accepts(request.vars,...):
id=create_parent(form)
request.vars.name.infile.seek(0) # rewind uploaded file
db.infile.insert(parent=id,
name=db.infile.name.store(request.vars.name.file,request.vars.name.filename))
process(id)
redirect( ...)
# and here's the routine for processing the input file to generate the
output file
def process(id):
"""Flattens infile and writes outfile"""
# I want to read the input file that was uploaded and then write
# a flattened version of the uploaded file to a path /myapp/static/
output/ab/xy/out_abjljsdfljljsdf.xykjsdkjhsf.txt
# I've start with this:
infile=db(db.file.parent==id).select(db.file.name).first()
inpath=get_full_path(infile.name)
stream=open(inpath,'rb')
stream=TAG(stream).flatten() # THIS IS WHERE THE PROBLEM IS
outpath=create_empty_file_under_static_slash_txtfiles(suffix='txt')
db.outfile.insert(parent=id,
name=db.outfile.name.store(stream,outpath)) # THIS DOESN'T WORK
BECAUSE IT'S NOT A STREAM
return
I've embedded comments above showing where I'm running into problems.
What's the best way for me to be able to flatten the input file and
write to the output file. NOTE: I am doing things this way because I
also want the input and output files to be autodeleted in case the
parent record is deleted.
p.s. unless I'm doing things the right way, which I doubt, don't worry
about syntax too much above since I just typed it into this post and
didn't copy and paste from a running program.