shrinerb / shrine

File Attachment toolkit for Ruby applications

Home Page:https://shrinerb.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot access to the tempfile path

lumir opened this issue · comments

Brief Description

I'm trying to access to the tempfile path in order to run a virus scanner in a before_validation callback but I don't see a method or a way to get it from the current documentation

include DocumentUploader::Attachment(:document)

before_validation :scan_for_viruses

def scan_for_viruses
    path = self.document.tempfile.path #something like this
    return if Clamby.safe?(path)
    File.delete(path)
end

Expected behavior

A method that return the tempfile path to pass it to the VirusScanner library

Actual behavior

Unable to find that method or way to get the tempfile path.

System configuration

Ruby version: 2.7.0

Shrine version: 2.19.4

I think this is best as a question in the forum!

There are several potential ways to tackle it. There isn't always an existing "tempfile path", depending on the way the file has been submitted and exactly what has happened in the process at the point you are acting.

The most general way to get a file with a path might be:

self.document.download do |tempfile|
  tempfile.path
end

Depending on circumstances this may or may not create an additional copy of your file.

I'd recommend asking any followup questions in the discussion forum, as the instructions here said to do with questions! https://discourse.shrinerb.com/