librelist archives

« back to archive

Serving a file from a temporary subdirectory

Serving a file from a temporary subdirectory

From:
rory donovan
Date:
2015-07-14 @ 19:14
I'm trying to let users download a file from a temporary directory I create
to process their uploads.  I have the whole upload/process/download
pipeline working, but only when processing all the files in the same
'uploads' directory, which is obviously not a great idea, since file name
collisions will occur. Everything goes fine with the uploads and processing
step, but when I click on the link to download the file, I get a 500 not
found, even though I can see the file sitting there in the directory via
ssh.

This is how things look in my main app file

# global place to make all the temporary sub-directories
tempfile.tempdir = os.path.abspath('uploads/')

@app.route('/uploads/<tmp_dir>/<filename>')
def serve_file_tmp_dir(tmp_dir,filename):
    return send_from_directory(tmp_dir,filename)

temp_dir = 'some_temp_dir'        # in uploads directory
temp_file = 'some_temp_file.txt'        # in temp_dir
serve_file_tmp_dir(os.path.abspath(os.path.join('uploads',temp_dir)),temp_file)

In my template file, the link
<a href="{{url_for('uploads')+'/some_temp_dir/some_temp_file.txt'}}">temp
file</a>
does point where it should, but when I click on it, I get a not found error.

Since the file is where's supposed to be, and the link points to that
place, I there must be some error in how I serve the file.  I feel like
maybe I haven't configured something properly, but I can't figure out the
issue.  Any suggestions?


Rory

Re: [flask] Serving a file from a temporary subdirectory

From:
Smith Ellis
Date:
2015-07-14 @ 22:07
Is it a permission issue?  Does your flask user have read and write on the
directory?  Always something like needing a chmod tripping me up.....

On Tue, Jul 14, 2015, 3:18 PM rory donovan <donovanr@alumni.reed.edu> wrote:

> I'm trying to let users download a file from a temporary directory I
> create to process their uploads.  I have the whole upload/process/download
> pipeline working, but only when processing all the files in the same
> 'uploads' directory, which is obviously not a great idea, since file name
> collisions will occur. Everything goes fine with the uploads and processing
> step, but when I click on the link to download the file, I get a 500 not
> found, even though I can see the file sitting there in the directory via
> ssh.
>
> This is how things look in my main app file
>
> # global place to make all the temporary sub-directories
> tempfile.tempdir = os.path.abspath('uploads/')
>
> @app.route('/uploads/<tmp_dir>/<filename>')
> def serve_file_tmp_dir(tmp_dir,filename):
>     return send_from_directory(tmp_dir,filename)
>
> temp_dir = 'some_temp_dir'        # in uploads directory
> temp_file = 'some_temp_file.txt'        # in temp_dir
>
> serve_file_tmp_dir(os.path.abspath(os.path.join('uploads',temp_dir)),temp_file)
>
> In my template file, the link
> <a href="{{url_for('uploads')+'/some_temp_dir/some_temp_file.txt'}}">temp
> file</a>
> does point where it should, but when I click on it, I get a not found
> error.
>
> Since the file is where's supposed to be, and the link points to that
> place, I there must be some error in how I serve the file.  I feel like
> maybe I haven't configured something properly, but I can't figure out the
> issue.  Any suggestions?
>
>
> Rory
>

Re: [flask] Serving a file from a temporary subdirectory

From:
rory donovan
Date:
2015-07-14 @ 22:22
Hmm, well to get the server to point to port 80, I'm using sudo python
myapp.py to run. Could that be an issue?  I would assume sudo would give me
too many permissions, rather than too few, but there could be something
subtle going on. I have no trouble serving files that are directly in the
uploads folder, for instance.

On 6:12PM, Tue, Jul 14, 2015 Smith Ellis <smithe@gmail.com> wrote:

> Is it a permission issue?  Does your flask user have read and write on the
> directory?  Always something like needing a chmod tripping me up.....
>
> On Tue, Jul 14, 2015, 3:18 PM rory donovan <donovanr@alumni.reed.edu>
> wrote:
>
>> I'm trying to let users download a file from a temporary directory I
>> create to process their uploads.  I have the whole upload/process/download
>> pipeline working, but only when processing all the files in the same
>> 'uploads' directory, which is obviously not a great idea, since file name
>> collisions will occur. Everything goes fine with the uploads and processing
>> step, but when I click on the link to download the file, I get a 500 not
>> found, even though I can see the file sitting there in the directory via
>> ssh.
>>
>
>> This is how things look in my main app file
>>
>> # global place to make all the temporary sub-directories
>>
> te! mpfile.te mpdir = os.path.abspath('uploads/')
>>
>
>> @app.route('/uploads/<tmp_dir>/<filename>')
>> def serve_file_tmp_dir(tmp_dir,filename):
>>     return send_from_directory(tmp_dir,filename)
>>
>> temp_dir = 'some_temp_dir'        # in uploads directory
>> temp_file = 'some_temp_file.txt'        # in temp_dir
>>
>> serve_file_tmp_dir(os.path.abspath(os.path.join('uploads',temp_dir)),temp_file)
>>
>> In my template file, the link
>> <a href="{{url_for('uploads')+'/some_temp_dir/some_temp_file.txt'}}">temp
>> file</a>
>> does point where it should, but when I click on it, I get a not found
>> error.
>>
>> Since the file is where's supposed to be, and the link points to that
>> place, I there must be some error in how I serve the file! .  I fee l like
>> maybe I haven't configured something properly, but I can't figure out the
>> issue.  Any suggestions?
>>
>>
>> Rory
>>
>

Re: [flask] Serving a file from a temporary subdirectory

From:
Smith Ellis
Date:
2015-07-14 @ 23:05
Try serving out of your static folder if you aren't.  Swinging at fences.

On Tue, Jul 14, 2015, 6:27 PM rory donovan <donovanr@alumni.reed.edu> wrote:

> Hmm, well to get the server to point to port 80, I'm using sudo python
> myapp.py to run. Could that be an issue?  I would assume sudo would give me
> too many permissions, rather than too few, but there could be something
> subtle going on. I have no trouble serving files that are directly in the
> uploads folder, for instance.
>
> On 6:12PM, Tue, Jul 14, 2015 Smith Ellis <smithe@gmail.com> wrote:
>
>> Is it a permission issue?  Does your flask user have read and write on
>> the directory?  Always something like needing a chmod tripping me up.....
>>
>> On Tue, Jul 14, 2015, 3:18 PM rory donovan <donovanr@alumni.reed.edu>
>> wrote:
>>
>>> I'm trying to let users download a file from a temporary directory I
>>> create to process their uploads.  I have the whole upload/process/download
>>> pipeline working, but only when processing all the files in the same
>>> 'uploads' directory, which is obviously not a great idea, since file name
>>> collisions will occur. Everything goes fine with the uploads and processing
>>> step, but when I click on the link to download the file, I get a 500 not
>>> found, even though I can see the file sitting there in the directory via
>>> ssh.
>>>
>>
>>> This is how things look in my main app file
>>>
>>> # global place to make all the temporary sub-directories
>>>
>> te! mpfile.te mpdir = os.path.abspath('uploads/')
>>>
>>
>>> @app.route('/uploads/<tmp_dir>/<filename>')
>>> def serve_file_tmp_dir(tmp_dir,filename):
>>>     return send_from_directory(tmp_dir,filename)
>>>
>>> temp_dir = 'some_temp_dir'        # in uploads directory
>>> temp_file = 'some_temp_file.txt'        # in temp_dir
>>>
>>> 
serve_file_tmp_dir(os.path.abspath(os.path.join('uploads',temp_dir)),temp_file)
>>>
>>> In my template file, the link
>>> <a
>>> href="{{url_for('uploads')+'/some_temp_dir/some_temp_file.txt'}}">temp
>>> file</a>
>>> does point where it should, but when I click on it, I get a not found
>>> error.
>>>
>>> Since the file is where's supposed to be, and the link points to that
>>> place, I there must be some error in how I serve the file! .  I fee l like
>>> maybe I haven't configured something properly, but I can't figure out the
>>> issue.  Any suggestions?
>>>
>>>
>>> Rory
>>>
>>

Re: [flask] Serving a file from a temporary subdirectory

From:
rory donovan
Date:
2015-07-15 @ 15:00
Your suggestion yielded some interesting behavior.  I haven't tried using
subdirectories, but when I serve a file out of static, and make a link to
it with
<a href="{{ url_for('static', filename=myfile) }}">

it just works, and I get a functioning link:
http://myserver.edu/static/myfile.zip


However, when I serve it out of uploads:
<a href="{{ url_for('uploads', filename=myfile) }}">

I get a nonfunctioning link:
http://myserver.edu/uploads?filename=myfile.zip

and have to resort to things link <a href="{{ url_for('uploads')+ '/' +
mayflies.zip) }}"> to get it to work.

Could this point to my uploads folder not being configured quite right?

Rory

On Tue, Jul 14, 2015 at 7:05 PM, Smith Ellis <smithe@gmail.com> wrote:

> Try serving out of your static folder if you aren't.  Swinging at fences.
>
> On Tue, Jul 14, 2015, 6:27 PM rory donovan <donovanr@alumni.reed.edu>
> wrote:
>
>> Hmm, well to get the server to point to port 80, I'm using sudo python
>> myapp.py to run. Could that be an issue?  I would assume sudo would give me
>> too many permissions, rather than too few, but there could be something
>> subtle going on. I have no trouble serving files that are directly in the
>> uploads folder, for instance.
>>
>> On 6:12PM, Tue, Jul 14, 2015 Smith Ellis <smithe@gmail.com> wrote:
>>
>>> Is it a permission issue?  Does your flask user have read and write on
>>> the directory?  Always something like needing a chmod tripping me up.....
>>>
>>> On Tue, Jul 14, 2015, 3:18 PM rory donovan <donovanr@alumni.reed.edu>
>>> wrote:
>>>
>>>> I'm trying to let users download a file from a temporary directory I
>>>> create to process their uploads.  I have the whole upload/process/download
>>>> pipeline working, but only when processing all the files in the same
>>>> 'uploads' directory, which is obviously not a great idea, since file name
>>>> collisions will occur. Everything goes fine with the uploads and processing
>>>> step, but when I click on the link to download the file, I get a 500 not
>>>> found, even though I can see the file sitting there in the directory via
>>>> ssh.
>>>>
>>>
>>>> This is how things look in my main app file
>>>>
>>>> # global place to make all the temporary sub-directories
>>>>
>>> te! mpfile.te mpdir = os.path.abspath('uploads/')
>>>
>>
>>> @app.route('/uploads/<tmp_dir>/<filename>')
>>> def serve_file_tmp_dir(tmp_dir,filename):
>>>     return send_from_directory(tmp_dir,filename)
>>>
>>> temp_dir = 'some_temp_dir'        # in uploads directory
>>> temp_file = 'some_temp_file.txt'        # in temp_dir
>>>
>>> 
serve_file_tmp_dir(os.path.abspath(os.path.join('uploads',temp_dir)),temp_file)
>>>
>>> In my template file, the link
>>> <a href="{{url_for('uploads'! )+'/s
>>> ome_temp_dir/some_temp_file.txt'}}">temp file</a>
>>> does point where it should, but when I click on it, I get a not found
>>> error.
>>>
>>> Since the file is where's supposed to be, and the link points to that
>>> place, I there must be some error in how I serve the file! .  I fee l like
>>> maybe I haven't configured something properly, but I can't figure out the
>>> issue.  Any suggestions?
>>>
>>>
>>> Rory
>>>
>>