Carrierwave pdf本地上传重复:雾。没有这样做:文件

时间:2017-07-05 23:20:56

标签: ruby-on-rails ruby pdf amazon-s3 carrierwave

我有一个非常奇怪的错误,我现在已经和他打了一天了。

我试图制作一个自动上传器,用于上传数百个PDF文件,并使用Carrierwave将它们从我的本地计算机分类到我的远程Amazon s3服务器。

当我用我的pdf_uploader中的文件运行时,一切都按原样运行,但是当我用雾运行它试图将它们保存在Amazon s3服务器上时,一切都是重复的。而不是像1,1,2,2,3,3但1,2,3,1,2,3

(我已经有两个image_uploaders,我之前尝试使用图片从本地目录上传,因此应该正确设置服务器和连接)

以下是我的文件:

#routes.rb 
get "upload_pdfs/now" => "pdf_categories#upload_pdfs", as: "upload_pdfs"
#pdf_categories_controller.rb
def upload_pdfs

    path = "/Users/bruger/Desktop/DENHER"

    Dir.foreach(path) do |category_path|

        next if folder_or_file_excluded? category_path
        pdf_category = PdfCategory.create!(title: category_path)

        Dir.foreach("#{path}/#{category_path}") do |file_path|  

            next if folder_or_file_excluded? file_path

            pdf_file_without_file_ending = remove_last_obj_in_arr file_path.split(".")


                new_pdf = Pdf.new(
                            title: pdf_file_without_ending,
                            pdf_category_id: pdf_category.id,
                            )

                #### SOMETHING IS HAPPENING HERE! ###
                File.open(path + "/" + category_path + "/" + file_path) do |file_from_path|
                    new_pdf.file = file_from_path
                    new_pdf.save!
                end
                #### SOMETHING IS HAPPENING HERE! ###

        end # file_path

    end # category_path
    redirect_to root_path
end

def is_integer? thing
    thing.to_i.to_s == thing
end 

def remove_last_obj_in_arr array
    new_array = array.first array.size - 1
    new_array.join(".")
end

def folder_or_file_excluded? folder_file
    list = %w(Fravalgt Fravalg Valgt fra fravalg fravalgt JPEG . .git DS_Store .. .DS_Store image_upload.rb)
    list.include? folder_file   
end
#pdf_uploader.rb
class PdfUploader < CarrierWave::Uploader::Base

      storage :fog

      def store_dir
        "pdf/#{mounted_as}/#{model.id}"
      end

      def extension_whitelist
        %w(pdf PDF)
      end

    end

您是否经历过这样的事情或者您是否有线索?

请。任何帮助将不胜感激!

0 个答案:

没有答案