forked from shrinerb/shrine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add refresh_metadata plugin for re-extracting metadata
- Loading branch information
Showing
6 changed files
with
94 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Shrine | ||
module Plugins | ||
# The `refresh_metadata` plugin allows you to re-extract metadata from an | ||
# uploaded file. | ||
# | ||
# plugin :refresh_metadata | ||
# | ||
# It provides `UploadedFile#refresh_metadata!` method, which calls | ||
# `Shrine#extract_metadata` with the uploaded file opened for reading, | ||
# and updates the existing metadata hash with the results. | ||
# | ||
# uploaded_file.refresh_metadata! | ||
# uploaded_file.metadata # re-extracted metadata | ||
# | ||
# For remote storages this will make an HTTP request to open the file for | ||
# reading, but only the portion of the file needed for extracting each | ||
# metadata value will be downloaded. | ||
module RefreshMetadata | ||
module FileMethods | ||
def refresh_metadata!(context = {}) | ||
refreshed_metadata = open { uploader.extract_metadata(self, context) } | ||
metadata.merge!(refreshed_metadata) | ||
end | ||
end | ||
end | ||
|
||
register_plugin(:refresh_metadata, RefreshMetadata) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "test_helper" | ||
require "shrine/plugins/refresh_metadata" | ||
|
||
describe Shrine::Plugins::RefreshMetadata do | ||
before do | ||
@uploader = uploader { plugin :refresh_metadata } | ||
end | ||
|
||
it "re-extracts metadata" do | ||
uploaded_file = @uploader.upload(fakeio("content", filename: "file.txt", content_type: "text/plain")) | ||
uploaded_file.metadata.delete("size") | ||
uploaded_file.refresh_metadata! | ||
assert_equal 7, uploaded_file.metadata["size"] | ||
assert_equal "file.txt", uploaded_file.metadata["filename"] | ||
assert_equal "text/plain", uploaded_file.metadata["mime_type"] | ||
end | ||
|
||
it "keeps any custom metadata" do | ||
uploaded_file = @uploader.upload(fakeio) | ||
uploaded_file.metadata["custom"] = "custom" | ||
uploaded_file.refresh_metadata! | ||
assert_equal "custom", uploaded_file.metadata["custom"] | ||
end | ||
|
||
it "forwards a Shrine::UploadedFile" do | ||
uploaded_file = @uploader.upload(fakeio) | ||
@uploader.class.plugin :add_metadata | ||
@uploader.class.add_metadata(:uploaded_file) { |io| io.is_a?(Shrine::UploadedFile) } | ||
uploaded_file.refresh_metadata! | ||
assert_equal true, uploaded_file.metadata["uploaded_file"] | ||
end | ||
|
||
it "accepts additional context and forwards it" do | ||
uploaded_file = @uploader.upload(fakeio) | ||
@uploader.class.plugin :add_metadata | ||
@uploader.class.add_metadata(:context) { |io, context| context.to_s } | ||
uploaded_file.refresh_metadata!(foo: "bar") | ||
assert_equal '{:foo=>"bar"}', uploaded_file.metadata["context"] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters