ruby-on-rails – 更改上传文件的tmp文件夹
我上传的所有文件都临时存储在文件夹/ 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 解决方法
如果设置TMPDIR,TMP,TEMP不起作用,则可能是您指定的目录不存在或不可写.或者$SAFE变量是> 0.使用函数Dir.tmpdir确定tmp文件夹(参见
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 Ruby 2.1 def Dir::tmpdir if $SAFE > 0 tmp = @@systmpdir else tmp = nil for dir in [ENV['TMPDIR'],'/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 因此,如果您要设置TMP env变量,请确保以下行为true > $SAFE == 0 设置tempdir的另一种方法是覆盖rails初始化程序中的tmpdir,但显然这会绕过任何目录检查,所以你必须确保它存在/可写 class Dir def self.tmpdir "/your_directory/" end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |