Bjo*_*sen 5 ruby rack file-upload ruby-on-rails
我上传的所有文件都临时存储在文件夹中/tmp。
我想更改这个文件夹,因为/tmp文件夹太小了。上传文件并在上传后将其移动到其他地方对我没有帮助。
我已经尝试过改变ENV['TMPDIR'],ENV['TMP']和ENV['TEMP']别的东西,但我上传的文件(* RackMultipart)仍暂存/tmp。
我怎样才能改变这种行为?当然,我可以将/tmp它挂载到其他地方,但是告诉 Rails/Rack/Thin/Apache/... 存储文件的位置会更容易。我没有使用回形针等。
对于我的服务器,我使用 Apache 作为代理平衡器将流量传递到 4 个瘦服务器。
我有一个使用 ruby 2.0 的 Rails 4 rc1 项目。
编辑:
def create
file = params[:sample_file][:files].first
md5_filename = Digest::MD5.hexdigest(file.original_filename)
samples = Sample.where("name in (?)", params["samples_#{md5_filename}"].map {|exp| exp.split(" (").first}) rescue []
file_kind = FileKind.find(params[:file_kind])
@sample_file = SampleFile.new
@sample_file.file_kind = file_kind
@sample_file.samples = samples
@sample_file.original_file_name = file.original_filename
@sample_file.uploaded_file = file #TODO: ..
@sample_file.user = current_user
...
#many other stuff
...
respond_to do |format|
if @sample_file.save
format.html {
render :json => [@sample_file.to_jq_upload].to_json,
:content_type => 'text/html',
:layout => false
}
format.json { render json: {files: [@sample_file.to_jq_upload]}, status: :created, location: @sample_file }
else
format.html { render action: 'new' }
format.json { render json: {files: [@sample_file.to_jq_upload]}.to_json, status: :ok}
end
end
end
Run Code Online (Sandbox Code Playgroud)
如果设置 TMPDIR,TMP,TEMP 不起作用,则可能是您指定的目录不存在或不可写。或者 $SAFE 变量 > 0。tmp 文件夹是使用函数 Dir.tmpdir 确定的(参见http://www.ruby-doc.org/stdlib-1.9.3/libdoc/tmpdir/rdoc/Dir.html#method -c-tmpdir)。
class Dir
def Dir::tmpdir
tmp = '.'
if $SAFE > 0
tmp = @@systmpdir
else
for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp']
if dir and stat = File.stat(dir) and stat.directory? and stat.writable?
tmp = dir
break
end rescue nil
end
File.expand_path(tmp)
end
end
end
Run Code Online (Sandbox Code Playgroud)
红宝石 2.1
def Dir::tmpdir
if $SAFE > 0
tmp = @@systmpdir
else
tmp = nil
for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp', '.']
next if !dir
dir = File.expand_path(dir)
if stat = File.stat(dir) and stat.directory? and stat.writable? and
(!stat.world_writable? or stat.sticky?)
tmp = dir
break
end rescue nil
end
raise ArgumentError, "could not find a temporary directory" if !tmp
tmp
end
end
Run Code Online (Sandbox Code Playgroud)
因此,如果您要设置 TMP 环境变量,请确保以下几行是正确的
设置 tempdir 的另一种方法是覆盖 Rails 初始值设定项中的 tmpdir,但显然这会绕过任何目录检查,因此您必须确保它存在/可写
class Dir
def self.tmpdir
"/your_directory/"
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6003 次 |
| 最近记录: |