#Locating on-disk location of a static file in-code

52 messages · Page 1 of 1 (latest)

vocal wadi
#

I'm building a basic file serving and managing system and want to have an error check to make sure a file exists on disk before serving. In my view, I'm able to build out urls using /static pretty easily, which refers back to one of several paths in a hierarchy of searches. Ideally, I would know pretty predictably where my managed files live because I'm putting them there.

Is there a way to de-render a static url /static/somefilename into a local POSIX-ish path /local/path/to/somefilename so I can do an os.path.exists() or other file operations on it?

tall canopy
#

How you expect to manage files that are static?

#

that in some point of view - is mutually exclusive

#

Case sounds really strange

vocal wadi
#

AHA, knowing where my /static folder is in an explicit path requires django.conf.static.BASE_DIR!

Use case (note that any uses of BASE_DIR is basically pseudocode, which is why I'm asking the question to know if that exists):

  • I upload an image to serve from a page.
  • The code on the backend receives the image uploaded and saves it to BASE_DIR/static/myimage.jpg along with storing metadata in a database somewhere (so the model might have the local path)
  • The page that would display it would load /static/myimage.jpg. As a safety precaution, I'd want to do os.path.exists(BASE_DIR + "/static/myimage.jpg") or something of that nature to make sure the file exists before serving up a broken image link.
  • I want to delete the image. Along with deleting the record from the database, I'd want to delete the file with os.remove(BASE_DIR + "/static/myimage.jpg")
tall canopy
#

I upload an image to serve from a page.
That is NOT static, which I was guessing is the case

#

So whole idea seem to need amending

#

os.path.exists(BASE_DIR + "/static/myimage.jpg")
that's also generally wrong

#

so

#

We are talking MEDIA files, media are counter part to STATIC with main difference of not being static but dynamic

#

Media are typically handled in models by FileField and ImageFIeld

#

which have attributes: obj.filefield.url and obj.filefield.path that represent respectively url to serve file and it's path on used storage (e.g. local filesystem)

#

Now further question - why are you afraid serving a broken file link?

#

The thing is that django is not supposed (and usually doesn't) serve files at all. So when you say "safety precaution" it's actually considerably less secure and efficient

vocal wadi
#

This is just for prototyping. If this goes into production, it'll be stored in and served from S3 buckets. It'll work perfectly for my use case.

#

And for what it's worth, it works just fine for me

vocal wadi
vocal wadi
tall canopy
tall canopy
#

So if it's what you are concerned about best to dump idea completely

vocal wadi
#

If I want to upload a file from a user form and then have that file be served, what then do I do?

#

I'm only doing what I think I should be doing from reading docs...

#

(files being images)

tall canopy
#

Just upload it and be happy

vocal wadi
#

??

tall canopy
#

Can make some code that periodically checks if file exists and not damages (e.g. verify checksum)

vocal wadi
#

you're missing my real end state, is that I want the file served to the frontend, which is why it makes sense to me to save the file to the /static/ folder... is there somewhere else that I should save the file that django will serve on request?

#

again, the use case here is images

tall canopy
#

files MUST NOT be uploaded to static

#

I'm not sure what are you reading in docs

#

They are called STATIC because they are not uploaded

#

You want MEDIA files

#

So you use an ImageField on model

#

Have a ModelForm for that model

#

then a view that handles model like:

form = MyImageUploadingForm(request.POST, request.FILES)
if form.is_valid():
    form.save()
#

that's all

#

image is uploaded

#

you can do in template <img src="{{ my_object.imagefield.url }}">

#

again, that's basically all

#

What would you even do if you parse image request url and find it's doesn't exists?

#

I'm not sure why are trying to make it complcated, I thought first you have some specific use case that may make that reasonable somehow, but it doesn't seem so now

vocal wadi
#

wait so you're saying I should store the image in the database and not write to disk?

tall canopy
#

No, I'm not saying that. ImageField doesn't store file in db, it stores it on file system

#

It (field itself) stores a relative path to file and provides attributes for easy operations with that file

#

like exists check

#
if os.path.isfile(my_object.imagefield.path):
    print('file is there!')
#

or getting .url to serve file on frontend

#

But in production django intentionally doesn't serve files. It's not django job, server like nginx or apache does it 10 times better

#

Django saves upload and makes the url, or some complex handling if needed

#

Serving is "peasant" job