janfri / mini_exiftool

This library is a wrapper for the Exiftool command-line application (https://exiftool.org) written by Phil Harvey. It provides the full power of Exiftool to Ruby: reading and writing of EXIF-data, IPTC-data and XMP-data. Branch master is for actual development and branch compatibility-version is for compatibility with Ruby 1.8 and exiftool versions prior 7.65.
GNU Lesser General Public License v2.1
213 stars 52 forks source link

Active storage #38

Closed iroshiva closed 4 years ago

iroshiva commented 4 years ago

Hi !

Is it possible to user mini_exiftool with a blob active storage ?

I tried to download the blob and put it in public folder but didn't figure it out the path to put it in MiniExiftool.new.

Tried open, but get an error message NoMethodError (private methodopen' called for MiniExiftool:Class):`

Don't understand how it works !!

Thx

janfri commented 4 years ago

Sorry for my late answer. I was in vacation.

I didn't know active storage. Could you give some code of what you have tried?

iroshiva commented 4 years ago

Pictures taken from some phones (iPhone, 1+, ...) are rotated 90° or -90° or 180° The main utilisation of mini_exiftool is to get the orientation of an image and modify it if necessary.

I use dropzone to download pictures. The pictures are first send to my AWS account, then from the request of each pictures uploaded, i get the active storage blob and then i attach it to my model.

What i want is get the active blob storage AND use mini_exiftool to read the orientation of the picture to be able to change it if the picture is wrong oriented BEFORE attch it to the model.

I tried to apply mini_exiftool directly after getting the active storage blob

` require 'mini_exiftool'

@user = User.find(params[:user_id])

blob = ActiveStorage::Blob.create!(upload_params)

movie = MiniExiftool.new(blob)
puts "#" * 60
puts movie.orientation
puts "#" * 60

@user.images.attach(blob.signed_id)`

But have this error : MiniExiftool::Error (Could not open filename_or_io.):

Then, i tried to store the blob to public file and then to read it, but no success...

`require 'mini_exiftool'

@user = User.find(params[:user_id])

blob = ActiveStorage::Blob.create!(upload_params)

id = @user.name.to_s
unless Dir.exists?(Rails.root.join("public", "#{id}"))
  FileUtils.mkdir_p(Rails.root.join("public", "#{id}")) 
end

time_footprint = Time.now.to_i.to_s(:number)
File.open(Rails.root.join('public', "#{id}", blob.filename.to_s), 'wb') do |file|
  File.rename(file, "public/#{id}/" + time_footprint + blob.filename.to_s)

  movie = MiniExiftool.new("/public/#{id}/" + time_footprint + blob.filename.to_s)
  puts "#" * 60
  puts movie.orientation
  puts "#" * 60
end

@user.images.attach(blob.signed_id)`

I have the error : MiniExiftool::Error (File '../public/admin/1598352666bantersnaps-y4bE8ST_CTg-unsplash.jpg' does not exist.):

The questions are:

Thx ! ;)

janfri commented 4 years ago

Could you try

blob.open do |file|
  movie = MiniExiftool.new(file)
  # ...
end
iroshiva commented 4 years ago

I got the error message : NoMethodError (private methodopen' called for #):`

janfri commented 4 years ago

As already mentioned don't I know active storage. I have no idea why ActiveStorage::Blob#open not work since the api doc has an example which uses this method.

In short: MiniExiftool.new needs a valid filename or an instance of IO as parameter. Certainly someone who knows active storage could help you out. Maybe you could ask on a Rails forum how to get a filename or an IO instance from an ActiveStorage::Blob instance.

iroshiva commented 4 years ago

Yep, gonna investigate ! ;)

To be sure, if i succeed accessing to the orientation of the picture, i can modify it with mini_exiftool ?

janfri commented 4 years ago

Yes, mini_exiftool can write tags. But writing is only supported for files not for IO instances. So you have to save the content of your ActiveStorage::Blob instance as file, work on this file with mini_exiftool, and update your ActiveStorage::Blob instance from that file.

iroshiva commented 4 years ago

So it would be more the second option that i tried.

I succeed to save the blob as a file, but didn't succeed to open it with mini_exiftool...

`@user = User.find(params[:user_id])

blob = ActiveStorage::Blob.create!(upload_params)

id = @user.name.to_s time_footprint = Time.now.to_i.to_s(:number)

File.open(Rails.root.join('public', "#{id}", blob.filename.to_s), 'wb') do |file| File.rename(file, "public/#{id}/" + time_footprint + blob.filename.to_s)

movie = MiniExiftool.new("/public/#{id}/" + time_footprint + blob.filename.to_s) puts "#" 60 puts movie.orientation puts "#" 60 end `

Get : MiniExiftool::Error (File '../public/admin/1598352666bantersnaps-y4bE8ST_CTg-unsplash.jpg' does not exist.):

Maybe, i mistake the path ??

janfri commented 4 years ago

Why do you rename the file? It should be possible to use blob.filename directly:

movie = MiniExiftool.new(blob.filename)

You need the file only temporarly.

janfri commented 4 years ago

I hope you have solved your problem and close this issue now. Feel free to reopen it when you need further help.