I need to get nginx compiled with the file upload package. The module doesn't come as part of the default nginx brew formula. It appears brew formulas are based off one download pkg, see below
class Nginx < Formula
homepage 'http://nginx.org/'
url 'http://nginx.org/download/nginx-1.2.0.tar.gz'
md5 'a02ef93d65a7031a1ea3256ad5eba626'
devel do
url 'http://nginx.org/download/nginx-1.3.0.tar.gz'
md5 'b02e171c4a088aa9a5ab387943ce08eb'
end
How can I download the bellow in a subfolder, say nginx/contrib ?
url 'http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz'
md5 '2681a6167551830a23336fa41bc539a1'
You can use a "subformula". You would stick this class definition in the Nginx formula file:
class NginxUploadModule < Formula
url 'http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz'
md5 '2681a6167551830a23336fa41bc539a1'
end
and then in the install method of the Nginx formula, you would do something like
NginxUploadModule.new.brew do
(buildpath/'where/you/want/the/files').install Dir['*']
end
adjusting the path accordingly.
There are a number of examples of this in the core Homebrew repository; grepping for "new.brew" should give you quite a few.
From https://github.com/Homebrew/homebrew/blob/master/Library/Contributions/example-formula.rb
# Additional downloads can be defined as resources and accessed in the
# install method. Resources can also be defined inside a stable, devel, or
# head block. This mechanism replaces ad-hoc "subformula" classes.
resource "additional_files" do
url "https://example.com/additional-stuff.tar.gz"
sha1 "deadbeef7890123456789012345678901234567890"
end
# Additional downloads can be defined as resources (see above).
# The stage method will create a temporary directory and yield
# to a block.
resource("additional_files").stage { bin.install "my/extra/tool" }
Related
I am looking for a way of running a type of check_access to my files located under my public folder. I heard about RAILS_ROOT/private. I believe this directory provides my demands.
But I don't have deep information about it. The core idea of mine is serving files to only use which has the ability to view/download the files. Such as a posted pictures, I don't want them to be public. Currently, all the people who have knowledge of the URL pointing to the correct directory can access all files.
PS: The files under /public dir are uploaded via carrierwave gem.
Thanks.
If you need access control for your files you don't want to serve them from your public folder. Your public folder is server either by ActionDispatch::StaticFile or directly by your web server - neither of which provide the kind of access controll you want.
Instead you would create a controller which serves up the files:
class FilesController < ActionController::Metal
before_action :authenticate! # you need to implement this
before_action :authorize! # you need to implement this
# GET /files/:filename
def show
path = Rails.root.join(
'uploads', # can be any directory you want really
# avoids a malicous user being able to use for example '../../secret/password'
ActiveStorage::Filename.new(params[:file_name]).sanitized
)
if File.exist?(path)
send_file path
else
head :not_found
end
end
end
You can define the permissions in your database and then you can add a check before displaying the url of a file to the users. You shouldn't keep the files in public folder if you want them to be restricted.
Hi I am newbie in rails and wanted to know any way to list folders and files for a url say public . So example user types example.com/public he gets all folders and files listed as we do in php if we start a wamp server . I need this so I can create files and have their url shared to public i.e. simply send url link online like example.com/public/test.pdf . Currently I am getting page with no routes defined .enter image description here
Thanks.
Create a controller to serve the file using a route parameter, ex.
get '/public/:filename', to: 'file#serve'
Then use the send_file method in your controller.
class FileController < ApplicationController
def serve
# For authorization:
#authorize!
# For authentication: (a way or another)
#redirect_to :access_denied unless user_signed_in?
send_file "/www/yourapp/var/files/#{params[:filename]}"
end
end
This way the file can be anywhere within your app, or even on a Cloud storage. It also gives you the ability to use authentication or authorization to check if the user has access.
Please note that this code is very simple, but there are much more options like Fog gem for Cloud storage and everything else.
See https://apidock.com/rails/ActionController/Streaming/send_file for more information.
I am trying to use an ECS Ops-Works layer to manage some automation, but I cannot figure out how to set up those instances to set up the ecs.config to contain my private Docker repository credentials, as one would do manually if managing ec2 jobs directly. I think I need to somehow use some custom Chef to override the setup recipe to load my template rather than the default template for that file, but I am new to Chef so how to do this is unclear.
So to restate the problem, you want to modify this template in the opsworks_ecs::setup recipe:
template "ecs.config" do
path "/etc/ecs/ecs.config"
source "ecs.config.erb"
owner "root"
group "root"
mode 0644
end
I don't know how you are 'calling' this but I'll assume for now that you're either putting this recipe directly in your run_list and/or calling it explicitly with include_recipe "opsworks_ecs::setup"
In that case, write a wrapper cookbook. If you work for "Acme, Org" it might be something like acme_opsworks_ecs::setup.
acme_opsworks_ecs/metadata.rb should at least have:
name 'acme_opsworks_ecs'
version '0.0.1'
depends 'opsworks_ecs'
acme_opsworks_ecs/recipes/setup.rb should look like:
include_recipe "opsworks_ecs::setup"
resources(template: "ecs.config").cookbook(cookbook_name)
acme_opsworks_ecs/templates/default/ecs.config.erb is also required
/* add your own template content to this file -- copy theirs and edit */
That should allow you to fix it. What you're doing is using Chef's two-pass parser so that the opsworks recipe defines the template resource, then you re-open it and edit it, before it actually runs. Now, wherever you have referenced opsworks_ecs::setup in your run_list or include_recipe calls, replace that recipe with acme_opsworks_ecs::setup.
If you don't directly call opsworks_ecs::setup, then wrap the opsworks recipe(s) that you do call instead following the same pattern.
If you google "chef-rewind" you can find more information about this kind of pattern of using chef. Note that the syntax that I used is built-in to chef though and does not require a custom gem install or chef_rewind resource/definition to use, so it will be substantially simpler to use the syntax in this answer.
My root is site/home/ubuntu/workspace/
Is it possible to access file (from browser by HTTP request) located inside workspace/ without configuring routes and controllers?
Does the question on 1) depend on file extension?
you can access path to your file like this:
File.expand_path("somestuff.rb", "~/workspace")
for me this code produces path as follows:
"/home/foodie/workspace/somestuff.rb"
I have this structure:
# /home/username/Workspace/rails_project/app/controllers/application_controller.rb
require "#{Rails.root}/../test.rb
And
# /home/username/Workspace/test.rb
# Some ruby code
As you can see, test.rbfile is outside RubyOnRails Project.
For security reason, you can not access a file outside project rails from URL without defining a route. What you can do, is point a route to controller that, based on file name provided at url, require a file outside rails project.
# /home/username/Workspace/rails_project/config/routes.rb
get '/get_file/:file_name', to: 'files#show'
# /home/username/Workspace/rails_project/app/controllers/files_controller.rb
class FilesController < ApplicationController
def show
document = params[:file_name]
send_data "#{Rails.root}/../#{document}, filename: document
end
end
For more info, see:
http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data
Without configuring routes and controllers, client can only access files in the public/ directory (it doesn't matter what the extension is). Bare in mind this: when your Rails app is run by webserver, its webroot will be the public directory, consequently to access public/file.ext you request should be webroot/file.ext
I have already built several Rake tasks to import a series of CSV files into my PostgreSQL database, but I've always had those tasks directed to a path on my local computer.
I need to have the tasks set up to access a secure on directory so my client can simply copy files into this directory, and when the Rake task is executed it automatically pulls from the online source.
This seems like it would be a simple adjustment in my code but I'm not sure how to make it work. If anyone has any suggested reading or can quickly help edit my code it would be appreciated.
Below is a copy of one of my Rake tasks:
require 'csv'
namespace :import_command_vehicles_csv do
task :create_command_vehicles => :environment do
puts "Import Command Vehicles"
csv_text = File.read('/Users/Ben/Sites/ror/LFD/command_vehicles.csv', :encoding => 'windows-1251:utf-8')
csv = CSV.parse(csv_text, :headers => true)
csv.each_with_index do |row,index|
row = row.to_hash.with_indifferent_access
CommandVehicle.create!(row.to_hash.symbolize_keys)
command_vehicle = CommandVehicle.last
if #report_timesheet_hash.key?(command_vehicle.report_nr)
command_vehicle.timesheet_id = "#{#report_timesheet_hash[command_vehicle.report_nr]}"
end
#command_vehicle.timesheet_id = #timesheet_id_array[index]
command_vehicle.save
end
end
end
You can create rake tast that can accept parameters and then you can pass whatever path you like.
Check out this link Set Multiple Environmental Variables On Invocation of Rake Task to see how to pass parameters via enviroment variables and this link http://viget.com/extend/protip-passing-parameters-to-your-rake-tasks to see bracket notation.
'/Users/Ben/Sites/ror/LFD/command_vehicles.csv' will only work on your local machine, unless you have an identical path to your resources on the remote host, which is hardly likely. As a result, you need to change the absolute path, to one that is relative to your file.
I have a folder hierarchy on my Desktop, stuck several folders down, like:
- test
| lib
| - test.rb
| config
| - config.yaml
And test.rb contains:
File.realpath('../config/config.yaml', File.dirname(__FILE__))
__FILE__ is a special variable that returns the absolute path to the script currently being interpreted. That makes it easy to find where something else is if you know where it exists relative to the current __FILE__.
The value returned by realpath will be the absolute path to the "config.yaml" file, based on its relative path from the current file. In other words, that would return something like:
/Users/ttm/Desktop/tests/junk/test/config/config.yaml
The nice thing about Rake is it's really using Ruby scripts, so you can take advantage of things like File.realpath. Also, remember that Rake's idea of the current-working directory is based on the location of the script being run, so everything it wants to access has to be relative to the script's location, or an absolute path, or you have to change Rake's idea of the current-working directory by using a chdir or cd.
Doing the later makes my head hurt eventually so I prefer to define some constants pointing to the required resources, and figuring out the real absolute path becomes easy doing it this way.
See "How can I get the name of the command called for usage prompts in Ruby?"
for more information.