使用Rails和Capistrano在控制器中获取共享路径

时间:2016-12-22 10:19:53

标签: ruby-on-rails ruby-on-rails-4 capistrano

我有一个上传系统,可以对上传的文件执行多次转换操作(需要真实的文件路径来执行这些操作),然后存储生成的文件的路径。

稍后将访问这些文件以创建zip文件(再次需要实际路径)

Capistrano的问题显然是当我使用Rails.root.join('public', 'uploads', 'designer_user_uploaded')存储文件路径然后尝试创建我的zip文件,就像10天后发布已更改,存储的文件路径不再指向正确的位置

我的目录位于Capistrano的链接目录中。因此文件不会丢失,但它们不能再被检索。

我不知道该如何应对。我认为通过像File.join(shared_path, 'public', 'uploads', 'designer_user_uploaded')之类的东西来解决这个问题会很容易,但是我无法找到如何从控制器获取共享路径。这也可能在开发中不起作用。

我在Rails 4.2.6

 output_folder = Rails.root.join('public', 'uploads', 'designer_user_uploaded')
          FileUtils.mkdir_p(output_folder) unless File.directory?(output_folder)

          file_name = sanitize_filename(Time.now.to_s + "_" + SecureRandom.uuid)
          original_file_path = output_folder.join(change_file_extension(file_name, ext))
          designer_file_path = ext == '.ai' || ext == '.eps' ?
              output_folder.join(change_file_extension(file_name, '.svg')) : output_folder.join(change_file_extension(file_name, ext))

          if ext == '.ai' || ext == '.eps'
            copy_file(file.path, original_file_path)
            copy_file(svg_path, designer_file_path)

            @response[:original_file_path] = original_file_path
            @response[:designer_file_path] = designer_file_path
            @response[:file_name] = file_name + '.svg'
            @response[:file_type] = "svg"
            @response[:colors] = get_image_colors(original_file_path)

            # Store created file paths for future access
            @up = UserUpload.create(designer_path: designer_file_path, original_path: original_file_path)
            puts @up.id
            @response[:file_id] = @up.id
          else
            copy_file(file.path, designer_file_path)

            @response[:original_file_path] = nil
            @response[:designer_file_path] = designer_file_path
            @response[:file_name] = file_name + ext
            @response[:file_type] = ext == '.svg' ? 'svg' : "image"
            @response[:colors] = get_image_colors(designer_file_path)

            # Store created file path for future access
            @up = UserUpload.create(designer_path: designer_file_path)
            puts @up.id
            @response[:file_id] = @up.id
          end

          # Embroidery conversion
          is_embroidery = params[:is_embroidery].to_s.downcase == 'true'
          if is_embroidery && (ext == '.svg' || ext == '.ai' || ext == '.eps')

            embroidery_file_name = "embroidery_#{file_name}.svg"
            embroidery_path = output_folder.join(embroidery_file_name)

            Embroidery::EmbroideryConversionJob.perform_async(original_file_path, embroidery_path, @up)
            @response[:designer_file_path] = embroidery_path
            @response[:file_name] = embroidery_file_name
            @response[:file_type] = "embroidery"
          end

          @response[:output_url] = '/uploads/designer_user_uploaded/'
          file.close(true)

0 个答案:

没有答案