From 8431ff2cc00f58def8a9e7de489c0e836d8b9d78 Mon Sep 17 00:00:00 2001 From: Jose Toro Date: Mon, 17 Jul 2023 16:21:50 +0200 Subject: [PATCH] feat(Filescan): Allow passing scan parameters (#31) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(Filescan): Allow passing scan parameters * Update filescan.go Co-authored-by: Marta Gómez Macías --------- Co-authored-by: Marta Gómez Macías --- filescan.go | 51 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/filescan.go b/filescan.go index 06be176..23444f1 100644 --- a/filescan.go +++ b/filescan.go @@ -44,15 +44,8 @@ type FileScanner struct { cli *Client } -// Scan sends a file to VirusTotal for scanning. The file content is read from -// the r io.Reader and sent to VirusTotal with the provided file name which can -// be left blank. The function also sends a float32 through the progress channel -// indicating the percentage of the file that has been already uploaded. The -// progress channel can be nil if the caller is not interested in receiving -// upload progress updates. An analysis object is returned as soon as the file -// is uploaded. -func (s *FileScanner) Scan(r io.Reader, filename string, progress chan<- float32) (*Object, error) { - +func (s *FileScanner) scanWithParameters( + r io.Reader, filename string, progress chan<- float32, parameters map[string]string) (*Object, error) { var uploadURL *url.URL var payloadSize int64 @@ -70,6 +63,14 @@ func (s *FileScanner) Scan(r io.Reader, filename string, progress chan<- float32 return nil, err } + if parameters != nil { + for key, val := range parameters { + if err := w.WriteField(key, val); err != nil { + return nil, err + } + } + } + w.Close() if payloadSize > maxFileSize { @@ -114,6 +115,38 @@ func (s *FileScanner) Scan(r io.Reader, filename string, progress chan<- float32 return analysis, nil } +// ScanParameters sends a file to VirusTotal for scanning. The file content is +// read from the r io.Reader and sent to VirusTotal with the provided file name +// which can be left blank. The function also sends a float32 through the +// progress channel indicating the percentage of the file that has been already +// uploaded. The progress channel can be nil if the caller is not interested in +// receiving upload progress updates. An analysis object is returned as soon as +// the file is uploaded. Additional parameters can be passed to the scan +// by using the parameters map[string]string argument. +func (s *FileScanner) ScanParameters( + r io.Reader, filename string, progress chan<- float32, parameters map[string]string) (*Object, error) { + return s.scanWithParameters(r, filename, progress, parameters) +} + +// ScanFileWithParameters sends a file to VirusTotal for scanning. This function +// is similar to ScanWithParameters but it receives an *os.File instead of a +// io.Reader and a file name. +func (s *FileScanner) ScanFileWithParameters( + f *os.File, progress chan<- float32, parameters map[string]string) (*Object, error) { + return s.scanWithParameters(f, f.Name(), progress, parameters) +} + +// Scan sends a file to VirusTotal for scanning. The file content is read from +// the r io.Reader and sent to VirusTotal with the provided file name which can +// be left blank. The function also sends a float32 through the progress channel +// indicating the percentage of the file that has been already uploaded. The +// progress channel can be nil if the caller is not interested in receiving +// upload progress updates. An analysis object is returned as soon as the file +// is uploaded. +func (s *FileScanner) Scan(r io.Reader, filename string, progress chan<- float32) (*Object, error) { + return s.scanWithParameters(r, filename, progress, nil) +} + // ScanFile sends a file to VirusTotal for scanning. This function is similar to // Scan but it receive an *os.File instead of a io.Reader and a file name. func (s *FileScanner) ScanFile(f *os.File, progress chan<- float32) (*Object, error) {