Use Shrine to upload video files and generate thumbnails - amazon-s3

I want to use the Shrine gem to upload video files, transcode, & generate thumbnails from the video.
I am trying to convert Erik Dahlstrand's Shrine-Rails-example from photos to videos. I am having trouble creating the video uploader. I based this code on Video isn't of allowed type (allowed types: video/mp4), Shrine, Rails
require "streamio-ffmpeg"
class VideoUploader < Shrine
ALLOWED_TYPES = %w[video/mp4 video/quicktime video/x-msvideo video/mpeg]
plugin :processing
plugin :versions
plugin :determine_mime_type
plugin :cached_attachment_data
plugin :remove_attachment
plugin :add_metadata
plugin :validation_helpers
plugin :derivation_endpoint, prefix: "derivations/video"
add_metadata do |io, context|
movie = Shrine.with_file(io) { |file| FFMPEG::Movie.new(file.path) }
{ "duration" => movie.duration,
"bitrate" => movie.bitrate,
"resolution" => movie.resolution,
"frame_rate" => movie.frame_rate }
end
movie.screenshot("video_thumb_007.jpg", seek_time: 5, resolution: '320x240')
metadata_method :duration
Attacher.validate do
validate_max_size 100.megabyte, message: "is too large (max is 100 MB)"
validate_mime_type_inclusion ALLOWED_TYPES
end
end
I get this error:
/var/folders/mm/_j8x4k2176jcv31zvbc497_c0000gp
/T/shrine20190607-24438-4f3jz2.m4v: No such file or directory
and in fact, there is no file in that location. So where is the file stored while waiting for upload??
Also, using the demo to upload photos to AWS (production env), the objects are stored in the bucket in a folder called 'photos'. Shrine uses the table name to name the folder apparently. Is possible to create alternative & nested folder names?
Thank you - seems like an amazing gem! Trying to understand it better!
Thx

Further delving into the documentation, I was able to solve this problem with only one remaining question - is it possible to control which folders in the AWS bucket the original and thumbs are uploaded to? Thx
Solution:
require "streamio-ffmpeg"
require "tempfile"
class VideoUploader < ImageUploader
plugin :add_metadata
plugin :validation_helpers
plugin :processing
plugin :versions
plugin :delete_raw
add_metadata do |io, context|
movie = Shrine.with_file(io) { |file| FFMPEG::Movie.new(file.path) }
{ "duration" => movie.duration,
"bitrate" => movie.bitrate,
"resolution" => movie.resolution,
"frame_rate" => movie.frame_rate
}
end
process(:store) do |io, context|
versions = {original: io}
io.download do |original|
screenshot1 = Tempfile.new(["screenshot1", ".jpg"], binmode: true)
screenshot2 = Tempfile.new(["screenshot2", ".jpg"], binmode: true)
screenshot3 = Tempfile.new(["screenshot3", ".jpg"], binmode: true)
screenshot4 = Tempfile.new(["screenshot4", ".jpg"], binmode: true)
movie = FFMPEG::Movie.new(original.path)
movie.screenshot(screenshot1.path, seek_time: 5, resolution: '640x480')
movie.screenshot(screenshot2.path, seek_time: 10, resolution: '640x480')
movie.screenshot(screenshot3.path, seek_time: 15, resolution: '640x480')
movie.screenshot(screenshot4.path, seek_time: 20, resolution: '640x480')
[screenshot1, screenshot2, screenshot3, screenshot4].each(&:open) # refresh file descriptors
versions.merge!(screenshot1: screenshot1, screenshot2: screenshot2, screenshot3: screenshot3, screenshot4: screenshot4)
end
versions
end
Attacher.validate do
validate_max_size 100.megabyte, message: "is too large (max is 100 MB)"
validate_mime_type_inclusion ALLOWED_TYPES
end
end

Related

How to attach images in mailer from active storage association in Rails

In rails 5.2, I have a model using has_many_attached :images. I would like to send out an email that includes all associated images as attachments.
My mailer method currently looks like:
def discrepancy_alert(asset_discrepancy_id, options={})
#asset_discrepancy = AssetDiscrepancy.find asset_discrepancy_id
#asset_discrepancy.images.each_with_index do |img,i|
attachments["img_#{ i }"] = File.read(img)
end
mail to: 'noone#gmail.com', subject: "email subject"
end
obviously, File.read does not work here because img is not a path, it is a blob. I have not been able to find any info on this in the docs
Question One:
Is there a rails method for attaching a blob like this?
I can use the following instead:
#asset_discrepancy.images.each_with_index do |img,i|
attachments["img_#{ i }"] = img.blob.download
end
Question Two:
the download method could use a log of RAM, is this usage ill advised?
It seems, with the addition of ActiveStorage, that rails mailers would have some new methods for interaction between the two....I have not seen anything in the docs. All the mailer attachments[] examples use paths to a local file.
in app/mailers/mailer.rb:
if #content.image.attached?
#filename = object.id.to_s + object.image.filename.extension_with_delimiter
if ActiveStorage::Blob.service.respond_to?(:path_for)
attachments.inline[#filename] = File.read(ActiveStorage::Blob.service.send(:path_for, object.image.key))
elsif ActiveStorage::Blob.service.respond_to?(:download)
attachments.inline[#filename] = object.image.download
end
end
in mailer view:
if #filename
image_tag(attachments[#filename].url)
else
image_tag(attachments['placeholder.png'].url)
end
This worked for me in production using Amazon S3.
in mailer view:
if #object.images
#object.images.each do |image|
path = "https://www.example.com" + Rails.application.routes.url_helpers.rails_blob_path(image, only_path: true)
<img src="<%=path%>">
end
end
Here is the working solution for active storage url in email template. I have seen the images visible in gmail. You can use "rails_blob_url". This works for file stored in aws s3.
mailer.rb
....
#image_url = Rails.application.routes.url_helpers.rails_blob_url(blob),
....
mailer view file
<img src="<%= #image_url %>">

create a (Prawn) PDF inside custom DelayedJob and upload it to S3?

Using: Rails 4.2, Prawn, Paperclip, DelayedJobs via ActiveJobs, Heroku.
I have a PDF that is very large and needs to be handled in the background. Inside a custom Job I want to create it, upload it to S3, and then email the user with a url when its ready. I facilitate this via a PdfUpload model.
Is there anything wrong with my approach/code? Im using File.open() as outlined in examples I found, but this seems to be the root of my error ( TypeError: no implicit conversion of FlightsWithGradesReport into String ).
class PdfUpload < ActiveRecord::Base
has_attached_file :report,
path: "schools/:school/pdf_reports/:id_:style.:extension"
end
/pages_controller.rb
def flights_with_grades_report
flash[:success] = "The report you requested is being generated. An email will be sent to '#{ current_user.email }' when it is ready."
GenerateFlightsWithGradesReportJob.perform_later(current_user.id, #rating.id)
redirect_to :back
authorize #rating, :reports?
end
/ the job
class GenerateFlightsWithGradesReportJob < ActiveJob::Base
queue_as :generate_pdf
def perform(recipient_user_id, rating_id)
rating = Rating.find(rating_id)
pdf = FlightsWithGradesReport.new( rating.id )
pdf_upload = PdfUpload.new
pdf_upload.report = File.open( pdf )
pdf_upload.report_processing = true
pdf_upload.report_file_name = "report.pdf"
pdf_upload.report_content_type = "application/pdf"
pdf_upload.save!
PdfMailer.pdf_ready(recipient_user_id, pdf_upload.id)
end
end
This results in an error:
TypeError: no implicit conversion of FlightsWithGradesReport into String
Changing this:
pdf_upload.report = File.open( pdf )
to this:
pdf_upload.report = StringIO.new(pdf.render)
fixed my problem.

How to store file and retrive it correctly with moodle file api?

i'm programming a mod activity for moodle which load files and show'em to any student who can access to the course.
The problem is that handing files in moodle is damn hard.
this is what i have done so far:
option page with importers
$mform->addElement('filepicker', 'slidesyncmedia', get_string('slidesyncmedia', 'slidesync'), null, array('maxbytes' => $maxbytes, 'accepted_types' => '*'));
$mform->addElement('filemanager', 'slidesyncslides', get_string('slidesyncslides', 'slidesync'), null, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50, 'accepted_types' => array('*') ));
after submit the files are stored in draft
and everything is loaded in another page that save all on db
if ($draftitemid = file_get_submitted_draft_itemid('slidesyncmedia')) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_slidesync', 'slidesyncmedia', 0, array('subdirs' => 0, 'maxfiles' => 1));
}
if ($draftitemid = file_get_submitted_draft_itemid('slidesyncslides')) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_slidesync', 'slidesyncslides', 0, array('subdirs' => 0, 'maxfiles' => 50));
}
in the end i use again the first page in another place (if files are there, then shows them)
$fs = get_file_storage();
if ($files = $fs->get_area_files($context->id, 'mod_slidesync', 'slidesyncslides', '0', 'sortorder', false)) {
// Look through each file being managed
foreach ($files as $file) {
// Build the File URL. Long process! But extremely accurate.
$fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
echo $fileurl;
}
} else {
echo '<p>Please upload an image first</p>';
}
this make an url but if clicked moodle says that file does not exist
mysite.com/pluginfile.php/53/mod_slidesync/slidesyncslides/0/Koala.jpg
in the db the file are correctly saved!!!
53 mod_slidesync slidesyncslides 0 / Koala.jpg
what i'm missing?
thanks
A long time passed but I was working on a plugin and had the same problem.
I manage to solve it.
To provide the file you need to create the:
function MYPLUGIN_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array())
Here is the example function: https://docs.moodle.org/dev/File_API#Serving_files_to_users
Remember change the last call to send_file to send_stored_file in Moodle 2.3+

Getting Origami-pdf to work with Amazon S3 files

I've implemented a local script to insert digital signatures into local pdf files recurring to Origami, but don't quite know what would be the best approach to do this within a rails server, and with amazon s3 stored files.
I am guessing i would need to download the file from s3 to my server (or capture it before uploading to amazon, which is what i am doing with paperclip) insert the signature, and sent it back to s3 again.
Here is the PDF.read method in pdf.rb file of origami solution:
class << self
#
# Reads and parses a PDF file from disk.
#
def read(filename, options = {})
filename = File.expand_path(filename) if filename.is_a?(::String)
PDF::LinearParser.new(options).parse(filename)
end
How could i adapt this so that i treat an in-memory binary file?
Do you have any suggestions?
You can find more about origami here
And my code below
require 'openssl'
begin
require 'origami'
rescue LoadError
ORIGAMIDIR = "C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\origami-1.2.4\lib"
$: << ORIGAMIDIR
require 'origami'
end
include Origami
INPUTFILE = "Sample.pdf"
#inputfile = String.new(INPUTFILE)
OUTPUTFILE = #inputfile.insert(INPUTFILE.rindex("."),"_signed")
CERTFILE = "certificate.pem"
RSAKEYFILE = "private_key.pem"
passphrase = "your passphrase"
key4pem=File.read RSAKEYFILE
key = OpenSSL::PKey::RSA.new key4pem, passphrase
cert = OpenSSL::X509::Certificate.new(File.read CERTFILE)
pdf = PDF.read(INPUTFILE)
page = pdf.get_page(1)
# Add signature annotation (so it becomes visibles in pdf document)
sigannot = Annotation::Widget::Signature.new
sigannot.Rect = Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
page.add_annot(sigannot)
# Sign the PDF with the specified keys
pdf.sign(cert, key,
:method => 'adbe.pkcs7.sha1',
:annotation => sigannot,
:location => "Portugal",
:contact => "myemail#email.tt",
:reason => "Proof of Concept"
)
# Save the resulting file
pdf.save(OUTPUTFILE)
PDF.read and PDF.save methods both accept either a file path or a Ruby IO object.
One method to create a PDF instance from a string (which, I suppose, is what you mean when you say "in-memory") is to use a StringIO object.
For example, the following session in the Origami shell will create a PDF instance, save it to a StringIO object and reload it using its own output string.
>>> PDF.new.save(strio = StringIO.new)
...
>>> strio.string
"%PDF-1.0\r\n1 0 obj\r\n<<\r\n\t/Pages 2 0 R ..."
>>> strio.reopen(strio.string, 'r')
#<StringIO:0xffbea6cc>
>>> pdf = PDF.read(strio)
...
>>> pdf.class
Origami::PDF
After a deep further analysis into the origami code, i noticed that PDF.Read accepts a binary file, and so instead of sending the local file path, we can send the file instance as a whole.
As #MrWater wrote, Origami::PDF.read accepts a stream (more precisely, the Origami::PDF::LinearParser does, look at the source here).
Here's my simple solution:
require 'open-uri'
# pdf_url = 'http://someurl.com/somepdf.pdf'
pdf = Origami::PDF.read(URI.parse(pdf_url))
References
open-uri URI
Origami::PDF::LinearParser

How do I export PSDs as PNGs with py-appscript?

I wrote a script to export PSDs as PNGs with rb-appscript. That's fine and dandy, but I can't seem to pull it off in py-appscript.
Here's the ruby code:
#!/usr/bin/env ruby
require 'rubygems'
require 'optparse'
require 'appscript'
ps = Appscript.app('Adobe Photoshop CS5.app')
finder = Appscript.app("Finder.app")
path = "/Users/nbaker/Desktop/"
ps.activate
ps.open(MacTypes::Alias.path("#{path}guy.psd"))
layerSets = ps.current_document.layer_sets.get
# iterate through all layers and hide them
ps.current_document.layers.get.each do |layer|
layer.visible.set false
end
layerSets.each do |layerSet|
layerSet.visible.set false
end
# iterate through all layerSets, make them visible, and create a PNG for them
layerSets.each do |layerSet|
name = layerSet.name.get
layerSet.visible.set true
ps.current_document.get.export(:in => "#{path}#{name}.png", :as => :save_for_web,
:with_options => {:web_format => :PNG, :png_eight => false})
layerSet.visible.set false
end
And here's the apparently nonequivalent python code:
from appscript import *
from mactypes import *
# fire up photoshop
ps = app("Adobe Photoshop CS5.app")
ps.activate()
# open the file for editing
path = "/Users/nbaker/Desktop/"
f = Alias(path + "guy.psd")
ps.open(f)
layerSets = ps.current_document.layer_sets()
# iterate through all layers and hide them
for layer in ps.current_document.layers():
layer.visible.set(False)
#... and layerSets
for layerSet in layerSets:
layerSet.visible.set(False)
# iterate through all layerSets, make them visible, and create a PNG for them
for layerSet in layerSets:
name = layerSet.name()
layerSet.Visible = True
ps.current_document.get().export(in_=Alias(path + name + ".png"), as_=k.save_for_web,
with_options={"web_format":k.PNG, "png_eight":False})
The only part of the python script that doesn't work is the saving. I've gotten all sorts of errors trying different export options and stuff:
Connection is invalid... General Photoshop error occurred. This functionality may not be available in this version of Photoshop... Can't make some data into the expected type...
I can live with just the ruby script, but all of our other scripts are in python, so it would be nice to be able to pull this off in python. Thanks in advance internet.
Bert,
I think I have a solution for you – albeit several months later. Note that I'm a Python noob, so this is by no means elegant.
When I tried out your script, it kept crashing for me too. However by removing the "Alias" in the export call, it seems to be OK:
from appscript import *
from mactypes import *
# fire up photoshop
ps = app("Adobe Photoshop CS5.1.app")
ps.activate()
# open the file for editing
path = "/Users/ian/Desktop/"
f = Alias(path + "Screen Shot 2011-10-13 at 11.48.51 AM.psd")
ps.open(f)
layerSets = ps.current_document.layer_sets()
# no need to iterate
ps.current_document.layers.visible.set(False)
ps.current_document.layer_sets.visible.set(False)
# iterate through all layerSets, make them visible, and create a PNG for them
for l in layerSets:
name = l.name()
# print l.name()
l.visible.set(True)
# make its layers visible too
l.layers.visible.set(True)
# f = open(path + name + ".png", "w").close()
ps.current_document.get().export(in_=(path + name + ".png"), as_=k.save_for_web,
with_options={"web_format":k.PNG, "png_eight":False})
I noticed, too, that you iterate through all the layers to toggle their visibility. You can actually just issue them all a command at once – my understanding is that it's faster, since it doesn't have to keep issuing Apple Events.
The logic of my script isn't correct (with regards to turning layers on and off), but you get the idea.
Ian