-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request help with implementing file upload field #240
Comments
Hey, I think you may want to try using the hooks pre_save / post_save / pre_delete. You could write some kind of a mixin that can be shared for this. You should still have access to the full FD object here. Hope this helps. |
I agree, that's what I am doing now as a workaround in my application. However this is not a good reusable solution IMO. It requires the implementation of and/or coordination with preexisting pre_save / post_save / pre_delete hooks in the Node implementation, not in the Property. I cannot "just" add one or more, say, FileProperty's and have it just work, like any other property. |
BTW FileDepot is just one backing store. Any other would probably fine as well. |
Ok the thing is you don't want to override the property getter or setter. As setting and getting doesn't actually touch the database. So what your saying is you don't want to have to have a mixin or base class to handle this special kind of property it would be nice if there was some way of hooking the save on to the property its self. I will mention now that deflate is actually used (abused) in other places than saving, it's also used for making queries with filter(...) etc (i think). I guess the ideal solution would be to support some kind of on_save hook for the property its self? |
The attachment reflects my (possibly muddled) thinking on the deflate/inflate/getter/setter stuff. Note that FD's file_id is a hex uuid string. FileIntent represents the file stuff going into FD (returning a file_id), FileStorage is what comes out of FD given a file_id. |
@mjmare Any further progress on this? I wonder if it could be marked as a future enhancement (?) Let me see if I got this right: You are proposing a File Property which is supposed to be working like:
And then execution is blocked until the "property" makes sure that the file has been uploaded before it can substitute the file for its file id (?). I think that from the point of view of an OGM there are two things:
So, the property, could have things like where to accept files from, what sort of filename structure (maybe through regexp) would be valid, restrictions on file size, acceptable mime types, etc. And the content would abstract the value itself. That is, the file. In this way, accessing the property would return a file that is almost ready for consumption, rather than a string filename that still requires some work until its content can be accessed. And it is at this level that different file storage backends could be abstracted too. This is not exactly visible in neomodel but when you implement a It also sounds like something that might require special treatment for Batch Operations. For example, first create all entities with some file attached and then attach the files as a separate step (or make uploads asynchronous). What do you think? |
Sorry for the late reply. Indeed in an ideal world I'd be able to add an "attachment" property to a Neomodel class. E.g.:
this would store some reference (uuid) on the node and store the file contents+metadata in some store (I used File Depot).
would return some object with the file contents and some metadata (called a StoredFile in File Depot). Hope this clears it up a bit. |
@mjmare No worries, please see below:
A few notes which we can take into account further in the design of this property:
But yeah, seems like a useful thing to have. |
I think the filename is part of the metadata. Using a uuid makes file(name) manipulations easier. Also when storage is not on the file system (eg database), the filename may not ideal. And how would one handle uploads with identical filenames (but different file contents)?
I think Neomodel should assume the file is available on the server before setting a Node's attribute.
The package FileDepot (https://pypi.python.org/pypi/filedepot) does a lot already but is independent of neo4j/NEomodel. |
Forgive me for creating this issue, but I couldn't find a mailinglist or forum to discuss things like this.
Since Neo4j is not good at storing file blobs I thought it would be useful to implement a FileUploadProperty backed by the nice package FileDepot (https://pypi.python.org/pypi/filedepot). FD lets you store files in various backends and only a returned file_id is stored in your database.
What I would like to create is a Neomodel property that would accept a file object (or FD FileIntent). The property would store the file object in FD and store the file_id in Neo4j. When accessing the property it would retrieve the file from FD and return a FD FileStorage (a sort of file descriptor).
I thought it would be easy to implement this with a NeoModel property by overriding the inflate/deflate methods. It turns out this did not work since inflate/deflate only handles the (de)serialisation to the graph node. It seems I needed to intercept the getting and setting the property.
This code works:
But handling the setting/getting in the StructuredNode is not very elegant. Good enough for a one-off program, but not very reusable. What I would like to see is that the specialised getting and setting of the property could be implemented in the UploadedFileProperty so that it is contained and can be easily reused.
I couldn't figure out how to set the getter/setter in the property implementation. Any pointers would be appreciated.
While we are at it, thinking ahead. It would be very convenient if the Property could tie in with the delete hook and transaction manager. In that way, when the Structured node is deleted, the corresponding FD file could be deleted automatically. The same with the transaction manager: if the transaction is aborted, the corresponding FD file could be deleted as well.
I hope that you agree that a FileProperty could be a useful addition and that you could give it some thought. TIA
Marcel
The text was updated successfully, but these errors were encountered: