Replies: 3 comments 3 replies
-
|
I’m still trying to implement this but not sure how. Anyone knows a way to do it? |
Beta Was this translation helpful? Give feedback.
-
|
Hello and happy new year. I still need something like this, zip on upload/store and unzip on retrieval. |
Beta Was this translation helpful? Give feedback.
-
|
Ended up with something like this, only compressing on the store phase. Not very streaming fashion, because my small knowledge with good streaming practice.. class CompressedStorage < SimpleDelegator
def upload(io, ...)
io = zip(io)
super
end
def open(...)
io = super
unzip(io)
end
private
def zip(io)
# zipping logic
zip_stream = Zip::OutputStream.write_buffer do |zip|
entry = io.metadata['filename'] if io.respond_to?(:metadata)
zip.put_next_entry(entry || 'entry')
zip.write(io.read)
end
zip_stream.rewind
zip_stream
end
def unzip(io)
# unzipping logic
unzipped_io = nil
Zip::InputStream.open StringIO.new(io.read) do |is|
_entry = is.get_next_entry
unzipped_io = StringIO.new(is.read)
end
unzipped_io
end
endMetadata stored is the original not "zipped", which is fine as I wanted transparent zip/unzip to save space, and keep reference of original size and type of files. Unfortunately I had to add in the uploader class some glue to set the correct zip content type and extension on S3, as the S3 storage was picking the original content type on uploading from the metadata. def generate_location(io, **options)
location = super
location = "#{File.basename(location, '.*')}.zip" if storage_key == :store
location
end
plugin :upload_options, store: -> (io, options) do
{ content_type: 'application/zip' }
endNot the best implementation for sure, but is currently working. Thanks for your help and any extra tip will be welcome for sure 🙂 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I’m using shrine with activerecord plugin in a pure ruby application.
Is there a recommended way so content can be automatically zipped when stored, and unzipped when retrieving it?
Checking the docs seems I can manually zip the content using the process(:store) directive (Processing | Shrine) but not sure if there is something similar on the retrieve for unzipping.
Maybe there is already some feature/plugin that do this?
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions