Flask, images and serving them - python

Sorry if the question seems stupid. I'm currently trying to create a website using Flask. This website will let user entry with an images. Let's say there is a price, a title, a description and an image. My problem here is with the images. I can store all the information I need in the databse but I really don't know what to do about the images. For example, when people enter the website, I'd want to display a couple of those images.
BUT, where do I store them, how do I serve them when they are called ? (For example, if I look at a user profile I'd like to see his post, images, etc...) I just don't know what to do about those images. If someone could explain like i'm an idiot or link me some information about that.
(For example, when you look at someone's profil on instagram you'll see every on his images, that's what i'd like too achieve)
Thanks !

You could store the images on the filesystem of your web server, then configure your webserver to serve them statically (if using Apache, for example, this would be done via the Alias Directive).
This would involve mapping URLs (e.g. mysite.com/img/) to a directory on the web server's filesystem (e.g. /home/images). Of course, your webserver will most likely be serving a directory by default, for example /var/www/, and you could simply store the images here.
Where you store the images (and how they are served) will depend on things specific to your application, such as security (note that if you serve a particular directory on your filesystem, any client could potentially download the contents).
EDIT
As per your comment - This would depend mostly on the size of the available storage on your web server. Also keep in mind that you should use images of a reasonable size, and a compressed format such as PNG or GIF.

Related

Flask File Uploads to Unique Directory

I've built an application following the file upload process (more or less) along the lines of the Flask file upload documentation found here, https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/.
In this portion of the code, UPLOAD_FOLDER = '/path/to/the/uploads', this points to one, single directory where file uploads will live. The problem I'm trying to solve is when I deploy my app to a server there will be multiple, simultaneous users. With a single upload directory, users will collide when they upload files with the same names--a situation that will occur in my app.
What I want to do is create a unique temp directory that is unique to each browser session. So, user 1 would have their own unique temp directory and user 2 would have their own unique temp directory and so on.
In this case, I think there would not be any user collision. Can anyone please suggest how I would create such unique temp directories associated with each browser session in the file upload process? Something along the lines of UPLOAD_FOLDER = '/path/to/the/uploads/user1_session', etc for each unique user?
Ok, so lacking further information and any sort of view on what your code/program looks like this is the what I would recommend at the moment.
I am relatively new to programming as well so this might not be the best answer. But in my experience you really,really do not want to be creating multiple directories per user/per session. That is a bad idea. This is where databases comes in handy.
Now in regards to your problem the easiest/fastest way to resolve this issue is to look into how password salt and hashing is done.
Just hash and salt your filenames.
Here is a link that provides a simple yet through explanation on how it is done.

Django Server - How to prevent caching on csv files?

I have a server which generates some data on a daily basis. I am using D3 to visualise the data (d3.csv("path")).
The problem is I can only access the files if they are under my static_dir in the project.
However, if I put them there, they do eventually get cached and I stop seeing the updates, which is fine for css and js files but not for the underlying data.
Is there a way to put these files maybe in a different folder and prevent caching on them? Under what path will I be able to access them?
Or alternatively how would it be advisable to structure my project differently in order to maybe avoid this operation in the first place. Atm, I have a seperate process that generates the data and stores it the given folder which is independent from the server.
Many thanks,
Tony
When accessing the files you can always add ?t=RANDOM to the request in order to get a "new" data all the time.
Because the request (on the server-side) is "new" - there will be no cache, and from the client side it doesn't really matter.
To get a new random you can use Date.now():
url = "myfile.csv?t="+Date.now()

Multiple storage containers in Django

Is it possible in Django to store image files in different buckets/containers in external object storage OpenStack Swift?
I have an issue related with creating proper way to upload image files through REST service request. The user is able to POST 'Task' instance via first endpoint, containing name, description, owner_id etc. He is also able to POST images via another endpoint, which has a relation many to one with Task.
Images should be stored in OpenStack Swift in my case (server already exists, was set up etc.) in unique containers/buckets as follows: owner_id_task_id. It is related, that users can upload files with the same names and extensions, but different content.
Another part will be sending also to the same containers in OpenStack Swift a files from Celery worker tasks (some processing based on uploaded files).
My goal is to achieve dynamically-created/overrided at runtime container structure, for storing raw images, and also post-processed images.
Any ideas how this problem can be solved?
Thanks for the help!
Yes. This is possible. You can use FileField.storage to configure which storage to use in individual model fields.

Displaying arbitrary nested filesystem directories/file contents on site using Django

I have a directory structure on the filesystem on my server. It can contain an arbitrary level of nested subdirectories, themselves which eventually end up containing “reports”/“queries” which are basically just xml files containing data. I'd like to display this directory structure as a nested collapsible menu list view on my site with the "reports" being the leaves in the menu list view and displaying the data from those "reports" in another area of the page once clicked on. In each of the menu headings I’d like to display the directory name (actually, I have already configured a file that maps the directory names to a more user friendly display name).
The structure of the data on disk looks like this (where the categories and subcategories are directories on the filesystem and the queries represent the files whose information I want to display):
My overall question is:
What’s the best way to read the filesystem directory structure into a python data structure so that I can eventually display it on a website using collapsible nested menus to represent the directory structure.
Since the data is being collected from the filesystem to be displayed on a page request, I’m not sure how to use django templates (if I even should) to generate these collapsible menus since the html being generated for the list structure is read on a page request and can and often does change on the filesystem. I'm not sure how to get a static Django template to work with something like that.
What is the best front-end framework out there that integrates well with Django, and preferably supports nested menus or blinds to an arbitrary depth that would allow me to display these menus and eventually the output of the reports files?
Maybe I should I use a django template to display the initial top level of the directory structure and use ajax to grab the next (and next after that) sub-level of data when a user tries to drill down into a category/directory? Ideally, I’d like to avoid the latency of making a request and waiting for a response every time a list item is clicked so I’d like to build the entire page on the server before sending it to the client. But again, I’m not sure if it’s even possible to use django templates to display arbitrary data like this.
Any suggestions and advice are much appreciated, thanks.
PS: I think I can just construct the html programmatically to display the directory structure and send that to the client but I'd like to avoid doing it that way if possible.
Just a suggestion but I would consider keeping a database table of all of the entries with their disk path, file size, file name, etc. Fill that with a scheduled job. Then search in the dbms against those paths/names.
That allows you to show a little bit more information to a user, and makes everything on the django/front end side use tooling that you'd normally expect to work with.
Take the file system out of the equation and think about what the user really wants to be able to do, odds are they don't care about how it is stored on the server.

How to serve url to audio in the gae blobstore

I have audio file stored as blobs in google app engine's blobstore. I'm not sure how to get a good url to pass to the client side to play the blob. I would like to do something like the image library.
image.get_serving_url()
But, there is no audio module. So, is there a good way to get a url from a blob to play audio or even better any media?
The rendering of an image is done by the browser. It's the same for audio, the browser decides what to do with a resource you point it to. For that, you need to add the correct mime type[1] header. If the file already had the correct mime type set when being uploaded you don't need to do this manually.
As for serving the blob, you need to create a blobstore download handler:
http://code.google.com/appengine/docs/python/tools/webapp/blobstorehandlers.html#BlobstoreDownloadHandler
[1] http://en.wikipedia.org/wiki/Internet_media_type
I think what you're looking for is something like how S3 works, where the blobs you upload are automatically given a URL that can then be dropped directly in to the browser. Blobstore was designed to primarily give developers control over their URLs and fine grained control over access to the blobs. It does not have the facility to simply provide a URL based on, say, the blob reference. I think schuppe's answer is correct in describing what you need to do.
If you are interested in simply serving a blob to a user without any kind of authentication or restriction, it's not that hard to write a handler. The one that is in the documentation that schuppe referred you to will work ok, however, be careful, because it could open your app up to certain types of DOS attacks. Also, if you do it as the documentation does it, anyone who has one of your blob-reference strings can access any blob throughout your whole application, whether you mean to or not. Therefore you should build some additional access control around it.
Of course, if you're not concerned with controlling access to the data, that solutions is simple and will work fine.

Categories