-
|
Does anyone have a recommended pattern for creating empty files in the cache, rather than uploading them? My situation is that my users upload ZIP files, but then I extract them and add the extracted files to Shrine, not the original zipfile. My current approach is to create a temporary directory inside the cache directory, unzip into that, then open each file and attach it, but it feels messy, because there's an unnecessary copy, and it leaves these temp files lying around unnecessarily for the cache sweeper to clean up later on. I'd rather do something like create a tempfile with Shrine, ready to be managed, then get the ZIP extractor to write directly to the filename that Shrine decided upon. Then when I attach it and Shrine promotes the file, there's only one copy, so there's nothing to clean up, and I'm. not worrying about race conditions with Shrine backgrounding etc. So in summary: is there a way to get Shrine to create an UploadedFile record in cache that I could then write to "out of band" before promoting? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
Are you using file system storage? If yes, you could consider moving the originally extracted file into the cache storage. Something like: File.open("/path/to/extracted/file", "rb") do |file|
record.file_attacher.attach_cached(file, upload_options: { move: true })
endI sort of get what you're asking for, but I'm not sure if that's currently possible with Shrine. |
Beta Was this translation helpful? Give feedback.
-
|
if I were doing something like that (and I have done things like that!) I would create your temporary files in local system temp location, using ruby stdlib Don't try to use possibly remote Shrine Then I think you've found a good way to attach the created file(s) you do want to keep at the end, yup! |
Beta Was this translation helpful? Give feedback.
I found a solution that works, after realising that I do something similar elsewhere in my app:
After extracting the file manually into the cache area, I can then create an UploadedFile object and attach that, which then cleans up nicely after itself.