I have a rails 3.2.2 app that's got the net-sftp gem installed on it.
I've created a simple controller to test the most basic feature of uploading a file to remote sftp.
This is my controller:
class UploadsController < ApplicationController
require 'net/sftp'
def upload
Net::SFTP.start('host', 'root', :password => 'password') do |sftp|
#sftp = sftp # I've got a session object so that seems to work
# upload a file or directory to the remote host
sftp.upload!("/Users/kensodev/Desktop/ashrit.xml", "/domains/inbar-paz.com/html/test/ashrit.xml")
end
end
end
When I visit "localhost:3000/uploads/upload" path I get this error:
Net::SFTP::StatusException open /domains/inbar-paz.com/html/test/ashrit.xml (2, "no such file")
Maybe I got the paths wrong?
Thanks you for trying to help :)
Paz.
One of the directories in this path "/domains/inbar-paz.com/html/test/" doesn't not exist.
Related
I have a model called client_billing_file where I use Carrierwave to upload a CSV file like this:
mount_uploader :billing_file_name, UsageFileUploader
and I schedule a job to run 5 minutes after commiting the creation of a new record:
after_commit :generate_usage_file, on: :create
def generate_usage_file
Resque.enqueue_in(5.minutes, GenerateUsageFileQueue, id, admin.email)
end
This is my background job:
def self.perform(client_billing_file_id, email)
cbf = ClientBillingFile.find(client_billing_file_id)
filepath = cbf.billing_file_name.current_path
csv_file = CSV.read(filepath, headers: true)
.
.
.
end
This is working in my development and testing environments, but it fails when I try to open the CSV file in the staging environment (where it actually uploads the file to the S3 bucket). I checked the bucket and the file is getting uploaded to the specified directory correctly, but for some reason the job is throwing the following error:
Exception Errno::ENOENT
Error No such file or directory # rb_sysopen - my_path/my_file.csv
Versions:
Ruby 2.6.6
Rails 4.2.11
Carrierwave 0.8.0
Fog 1.38.0
I tried Jared Beck's idea and it's working now, basically I added this condition to my BG job:
if Rails.env.production? || Rails.env.staging?
url = cbf.billing_file_name.url
cbf.billing_file_name.download!(url)
end
So the final code looks like this:
def self.perform(client_billing_file_id, email)
cbf = ClientBillingFile.find(client_billing_file_id)
if Rails.env.production? || Rails.env.staging?
url = cbf.billing_file_name.url
cbf.billing_file_name.download!(url)
end
filepath = cbf.billing_file_name.current_path
csv_file = CSV.read(filepath, headers: true)
.
.
.
end
As a personal project, I decided to write a minified-version of Ruby on Rails and upload it as a gem using Bundle called railz_lite.
Inside of my project, I was hoping to implement a Generator similar to rails new, which would create the necessary folders for a web app i.e. controllers/, views/, models/, etc.
To do so, I included Thor as a dependency, then created the following files:
require 'thor/group'
module RailzLite
module Generators
class Project < Thor::Group
include Thor::Actions
def self.source_root
File.dirname(__FILE__) + "/templates"
end
def add_controllers
empty_directory("controllers")
end
def add_models
empty_directory("models")
end
def add_server
template("server.rb", "config/server.rb")
end
def add_views
empty_directory("views")
end
def add_public
empty_directory("public")
end
end
end
end
Inside the gem project's root folder, when I run bundle exec railz_lite new, the generator works just fine and the necessary files are created.
However, if I create a new project, puts my gem (railz_lite) in the Gemfile, run bundle install, then execute bundle exec rails_lite new, I am greeted with the following error:
.rbenv/versions/2.5.1/lib/ruby/2.5.0/fileutils.rb:232:in `mkdir':
: Read-only file system # dir_s_mkdir - /controllers (Errno::EROFS)
I suspect the error is because the empty_directory command is not referring to the root directory of the project I just created. I am hoping that there is a simple way to fix this.
For further reference, the CLI script and class look as follows:
railz_lite
#!/usr/bin/env ruby
require 'railz_lite/cli'
RailzLite::CLI.start
cli.rb
require 'thor'
require 'railz_lite'
require 'railz_lite/generators/project'
module RailzLite
class CLI < Thor
desc 'new', 'Generates a new RailzLite project'
def new
RailzLite::Generators::Project.start([])
end
end
end
Any solutions would be greatly appreciated!
Note: I am running this on macOS Catalina.
So I found a solution after searching extensively through gem forums and looking at the Rails source code.
Inside of the generation I have to manually set the destination_root to the working directory of the project. The working directory can be found with Dir.pwd
require 'thor/group'
module RailzLite
module Generators
class Project < Thor::Group
include Thor::Actions
def self.source_root
File.dirname(__FILE__) + "/templates"
end
def self.destination_root # this method fixes the problem!
Dir.pwd # get the current project directory
end
def add_controllers
empty_directory("controllers")
end
def add_models
empty_directory("models")
end
def add_server
template("server.rb", "config/server.rb")
end
def add_views
empty_directory("views")
end
def add_public
empty_directory("public")
end
end
end
end
The guide says that I can save an attachment to disc to run a process on it like this:
message.video.open do |file|
system '/path/to/virus/scanner', file.path
# ...
end
My model has an attachment defined as:
has_one_attached :zip
And then in the model I have defined:
def process_zip
zip.open do |file|
# process the zip file
end
end
However I am getting an error :
private method `open' called
on the zip.open call.
How can I save the zip locally for processing?
As an alternative in Rails 5.2 you can do this:
def process_zip
# Download the zip file in temp dir
zip_path = "#{Dir.tmpdir}/#{zip.filename}"
File.open(zip_path, 'wb') do |file|
file.write(zip.download)
end
Zip::File.open(zip_path) do |zip_file|
# process the zip file
# ...
puts "processing file #{zip_file}"
end
end
That’s an edge guide (note edgeguides.rubyonrails.org in the URL); it applies to the master branch of the rails/rails repository on GitHub. The latest changes in master haven’t been included in a released version of Rails yet.
You’re likely using Rails 5.2. Use edge Rails to take advantage of ActiveStorage::Blob#open:
gem "rails", github: "rails/rails"
In rails 4.2.0, I am using paperclip for file uploads. But it is throwing an error like Errno::EACCES (Permission denied # dir_s_mkdir - /files), how can I fix this issue?
When I run gem list paperclip, I got the list like below
paperclip (4.3.0, 4.2.2, 4.2.0, 2.4.5)
In controller, I have tried 2 ways, one is #file = Asset.new(:document=>params[:asset][:document]) and the other way is
#file = Asset.new(user_params)
def user_params
params.require(:asset).permit(:document)
end
In model,
attr_accessible :status, :document_file_name, :document_content_type, :document_file_size
attr_accessible :document
has_attached_file :document,
:url => '/files/:assetable_id/:basename.:extension',
:path => "/files/:assetable_id/:basename.:extension",
:storage => :filesystem
How can I solve this permission denied issue?
Change your path to the following (using :rails_root):
:path => ":rails_root/files/:assetable_id/:basename.:extension"
rails_root will give you the path to your app.
To create Directory on local drive here is the running code-
To do so I was executing-
Dir.mkdir(Rails.root+ '/' + 'export')
But getting error as-
Errno::EACCES: Permission denied # dir_s_mkdir - /Main_File
I know what was the reason, it was looking for Super User ($ sudo) permission but we can't provide machine password each time.
Following worked for me as required-
Dir.mkdir(File.join(Dir.home, ".foo"), 0700) #=> 0
To create and store the path in a variable-
main_file = File.exist?( File.join(Dir.home, "Main_File") ) ? File.join(Dir.home, "/Main_File") : Dir.mkdir( File.join(Dir.home, "Main_File") )
Above will create file if doesn't exist
If it exist will access that and store in variable main_file.
Thanks for this link!
Hope will work for you !
I am just tryout some new technologies and find out about abbyy gem I created a free account on http://ocrsdk.com/plans-and-pricing/
I am following the instruction on the gem
class Client < ActiveRecord::Base
def abbyy
client = Abbyy::Client.new
client.process_business_card self.business_card, exportFormat: 'xml', imageSource: 'photo'
# Errno::ENOENT: No such file or directory - https://appname-dev.s3.amazonaws.com/uploads/client/business_card/1/bizcard.jpg
client.get_task_status
client.get
end
end
but I am getting a this error
Errno::ENOENT: No such file or directory - https://appname-dev.s3.amazonaws.com/uploads/client/business_card/1/bizcard.jpg
I made sure that the directory I am uploading is public
here is a link to a demo app https://github.com/mzaragoza/abbyy
Add require 'open-uri' to the top of your file.
Then download the file, and only then give it to abby:
def abby
require 'tempfile'
card = Tempfile.new('business_card')
card.binmode
stream = open(self.business_card.url)
card.write(stream.read)
stream.close
card.close
client = Abbyy::Client.new
client.process_business_card card.path, exportFormat: 'xml', imageSource: 'photo'
client.get_task_status
client.get
ensure
# ensuring every handle is closed, and ignoring exceptions, which could arise if handles already closed
# or haven't been opened
stream.close rescue nil
card.close rescue nil
card.unlink rescue nil
end