For paperclip I have a file that I add programmatically. The file is a generated PDF. So basically I don't need this file to be saved to my server's HDD. What I do now is the following:
#tempfile = Tempfile.open( ['','.pdf'], nil, 'wb', encoding: "ASCII-8BIT") do |file|
file << render_to_string( pdf: "pdf_file.pdf", layout: "pdf", template: "projects/generatePDF" )
end
#export.pdf = File.open( #tempfile.path ) # Paperclip Attachment
These are three steps: Create, write, open. So I'm wondering if we can do this easier, something like the following would be great:
#export.pdf = File.new( render_to_string( pdf: "pdf_file.pdf", layout: "pdf", template: "projects/generatePDF" ) )
Try using a stringio - this is a subclass of IO that is backed by a string rather than a file.
Related
Sphinx defines a role :download: that instructs Sphinx to copy the reference file to _downloads.
Does Pandoc has a similar feature?
Pandoc does not have that feature built-in, but it can be added with a few lines of Lua:
local prefix = 'media'
local path = pandoc.path
function Code (code)
if code.attributes.role == 'download' then
local description, filename = code.text:match '(.*) %<(.*)%>$'
local mimetype, content = pandoc.mediabag.fetch(filename)
local mediabag_filename = path.join{
pandoc.utils.sha1(content),
path.filename(filename)
}
if content and mimetype then
pandoc.mediabag.insert(mediabag_filename, mimetype, content)
end
return pandoc.Link(description, path.join{prefix, mediabag_filename})
end
end
Use the script by saving it to a file download-role.lua and the call pandoc with
pandoc --lua-filter=download-role.lua --extract-media=media ...
This will also work when using Markdown:
`this example script <../example.py>`{role=download}
I am testing adding a watermark to a video once uploaded. I am running into an issue where lamdba wants me to specify which file to change on upload. but i want it to trigger when any (really, any file that ends in .mov, .mp4, etc.) file is uploaded.
To clarify, this all works manually in creating a pipeline and job.
Here's my code:
require 'json'
require 'aws-sdk-elastictranscoder'
def lambda_handler(event:, context:)
client = Aws::ElasticTranscoder::Client.new(region: 'us-east-1')
resp = client.create_job({
pipeline_id: "15521341241243938210-qevnz1", # required
input: {
key: File, #this is where my issue
},
output: {
key: "CBtTw1XLWA6VSGV8nb62gkzY",
# thumbnail_pattern: "ThumbnailPattern",
# thumbnail_encryption: {
# mode: "EncryptionMode",
# key: "Base64EncodedString",
# key_md_5: "Base64EncodedString",
# initialization_vector: "ZeroTo255String",
# },
# rotate: "Rotate",
preset_id: "1351620000001-000001",
# segment_duration: "FloatString",
watermarks: [
{
preset_watermark_id: "TopRight",
input_key: "uploads/2354n.jpg",
# encryption: {
# mode: "EncryptionMode",
# key: "zk89kg4qpFgypV2fr9rH61Ng",
# key_md_5: "Base64EncodedString",
# initialization_vector: "ZeroTo255String",
# },
},
],
}
})
end
How do i specify just any file that is uploaded, or files that are a specific format? for the input: key: ?
Now, my issue is that i am using active storage so it doesn't end in .jpg or .mov, etc., it just is a random generated string (they have reasons for doing this). I am trying to find a reason to use active storage and this is my final step to making it work like other alternatives before it.
The extension field is Optional. If you don't specify anything in it, the lambda will be triggered no matter what file is uploaded. You can then check if it's the type of file you want and proceed.
From file access_file.txt, which has around 500 entries, with the content:
id\hzxcr
roll\85pol
id\byt65_d
rfc\myid
sub\aa_frt_09
.........
.........
I want to check if any of its lines is present in any of the files under the directory :D:/Details/Ruby_new, which has around 100+ files, with an extension ending i.e., *-accessfile.txt, as follows:
Name_accessfile.txt
ID_accessfile.txt
domain_accessfile.txt
roll_accessfile.txt
.......
.......
If present, I want to delete that instance or string, and save it in the same file. I don't want to create a new file or a backup file, but edit and save in the same file.
I came up with the following code:
value=File.open('D:\\my_work\\access_file.txt').read
value.gsub!(/\r\n?/, "\n")
value.each_line do |line|
line.chomp!
Dir.glob("D:/Details/Ruby_new/*-accessfile.txt") do |file_name|
text = File.read(file_name)
#print "FileName: #{file_name}\n"
replace = text.gsub(/#{line}/, "")
File.open(file_name, "w") { |file| file.puts replace }
end
end
but I'm facing the following warning, and the string is not removed from the target files.
my_ruby.rb:10: warning: invalid subexp call: /id\hzxcr/ my_ruby:10:
warning: invalid subexp call: /id\hzxcr/
Looking for any suggestions.
Try this replace = text.gsub(line.strip, "") instead of replace = text.gsub(/#{line}/, "")
Right now I am able to do this:
#template = Liquid::Template.parse("hi {{name}}") # Parses and compiles the template
p #template.render('name' => 'tobi')
However, how can I call Liquid::Template with a file that I have called template.liquid that has the content:
hi {{name}}
File.read() returns the whole file as a string, so you can write:
template = Liquid::Template.parse(
File.read("template.liquid")
)
I'm using the (Axlsx gem and it's working great, but I need to add an image to a cell.
I know it can be done with an image file (see Adding image to Excel file generated by Axlsx.?), but I'm having a lot of trouble using our images stored in S3 (through Carrierwave).
Things I've tried:
# image.url = 'http://.../test.jpg'
ws.add_image(:image_src => image.url,:noSelect => true, :noMove => true) do |image|
# ArgumentError: File does not exist
or
ws.add_image(:image_src => image,:noSelect => true, :noMove => true) do |image|
# Invalid Data #<Object ...>
Not sure how to proceed
Try using read to pull the contents into a tempfile and use that location:
t = Tempfile.new('my_image')
t.binmode
t.write image.read
t.close
ws.add_image(:image_src => t.path, ...
To add an alternative answer for Paperclip & S3 as I couldn't find a reference for that besides this answer.
I'm using Rails 5.0.2 and Paperclip 4.3.1.
With image URLs like: http://s3.amazonaws.com/prod/accounts/logos/000/000/001/original/logo.jpg?87879987987987
#logo = #account.company_logo
if #logo.present?
#logo_image = Tempfile.new(['', ".#{#logo.url.split('.').last.split('?').first}"])
#logo_image.binmode # note that our tempfile must be in binary mode
#logo_image.write open(#logo.url).read
#logo_image.rewind
end
In the .xlsx file
sheet.add_image(image_src: #logo_image.path, noSelect: true, noMove: true, hyperlink: "#") do |image|...
Reference link: http://mensfeld.pl/tag/tempfile/ for more reading.
The .split('.').last.split('?').first is to get .jpg from logo.jpg? 87879987987987.