当看到“Getting Errno::ENOENT”时,如何在 Rails 中获取正确的 Tempfile 文件路径没有这样的文件或目录@rb_sysopen“”

发布于 2025-01-11 17:43:34 字数 1468 浏览 4 评论 0原文

我想在我的网站上显示演示文稿文件的预览。我正在尝试创建一个临时文件,该文件从存储在活动存储中的 Microsoft PowerPoint Open XML (.pptx) 文件中读取。

我在临时文件上使用 Docsplit.extract_images 来将幻灯片转换为图像,以某种形式的图像轮播将其显示为预览。

我有幻灯片参数,例如 [:name, :ppt,pages: [] ],其中 ppthas_one_attached ,pages 是 has_many_attached。这就是我的 Slides_controller 的样子:

def create
    @slide = Slide.new(slide_params)

    respond_to do |format|
      if @slide.save
        tempfile = Tempfile.new([ 'foobar', '.pptx'])
        tempfile.binmode

        begin
          @slide.ppt.download { |chunk| tempfile.write(chunk) }
          tempfile.flush
          tempfile.rewind
        ensure
          tempfile.close!
        end
        @slide.pages << Docsplit.extract_images("#{tempfile.path}", :size => '1080x', :format => [:png])
        tempfile.unlink

        format.html { redirect_to slide_url(@slide), notice: "Slide was successfully created." }
        format.json { render :show, status: :created, location: @slide }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @slide.errors, status: :unprocessable_entity }
      end
    end
  end

我收到 Errno::ENOENT no such file or directory @ rb_sysopen 错误。

这是获取 tempfile 路径的正确方法吗?

另外,如果我使用 tempfile.path 而不是 "#{tempfile.path}",我会收到 nil to string 错误。

I want to show a preview of presentation files on my website. I am trying to make a tempfile which reads from a Microsoft PowerPoint Open XML (.pptx) file stored in active storage.

I am using Docsplit.extract_images on the tempfile to convert the slides to images to show it as a preview in some form of image carousel.

I have slide parameterss such as [:name, :ppt, pages: [] ] where ppt is has_one_attached and pages is has_many_attached. This is what my slides_controller look like:

def create
    @slide = Slide.new(slide_params)

    respond_to do |format|
      if @slide.save
        tempfile = Tempfile.new([ 'foobar', '.pptx'])
        tempfile.binmode

        begin
          @slide.ppt.download { |chunk| tempfile.write(chunk) }
          tempfile.flush
          tempfile.rewind
        ensure
          tempfile.close!
        end
        @slide.pages << Docsplit.extract_images("#{tempfile.path}", :size => '1080x', :format => [:png])
        tempfile.unlink

        format.html { redirect_to slide_url(@slide), notice: "Slide was successfully created." }
        format.json { render :show, status: :created, location: @slide }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @slide.errors, status: :unprocessable_entity }
      end
    end
  end

I am getting a Errno::ENOENT no such file or directory @ rb_sysopen error.

Is this the correct way to get the tempfile path?

Also, If I use tempfile.path instead of "#{tempfile.path}", I get a nil to string error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2025-01-18 17:43:34

确保您正在使用 tempfile.close!这将取消链接文件 https://ruby-doc.org/stdlib-2.5.3/libdoc/tempfile/rdoc/Tempfile.html#method-i-close-21

如果你只是使用 close 而没有一声巨响(!)那么你应该已经准备好了!

In your ensure you are using tempfile.close! this unlinks the file per https://ruby-doc.org/stdlib-2.5.3/libdoc/tempfile/rdoc/Tempfile.html#method-i-close-21

If you just use close without the bang (!) then you should be all set!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文