72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package files
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/spf13/viper"
|
|
|
|
"git.jmbit.de/filegate/filegate/db"
|
|
"git.jmbit.de/filegate/filegate/files/localfiles"
|
|
)
|
|
|
|
// DownloadFile Downloads file from a given URL, stores it in a Minio object. This function also calls the Static
|
|
// Analysis function
|
|
func DownloadFile(rawURL string, blob string, id uint) (uint, error) {
|
|
ctx := context.Background()
|
|
fileURL, err := url.Parse(rawURL)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return 0, err
|
|
}
|
|
response, err := http.Get(rawURL)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return 0, err
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
err := Body.Close()
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
}(response.Body)
|
|
|
|
path := fileURL.Path
|
|
splitPath := strings.Split(path, "/")
|
|
fileName := splitPath[len(splitPath)-1]
|
|
log.Printf("Downloading file %d, OG Name: %s, Path: %s \n", id, fileName, path)
|
|
contentType := response.Header.Get("content-type")
|
|
if contentType == "" {
|
|
contentType = "application/octet-stream"
|
|
}
|
|
db.UpdateOriginalName(id, fileName)
|
|
if viper.GetString("localfs") == "" {
|
|
objectInfo, err := MinioClient.PutObject(ctx, viper.GetString("minio.bucket"), blob, response.Body, response.ContentLength,
|
|
minio.PutObjectOptions{ContentType: contentType})
|
|
if err != nil {
|
|
log.Printf("Could not create File object: %v \n", err)
|
|
return 0, err
|
|
}
|
|
log.Printf("Successfully uploaded %s of size %d\n", blob, objectInfo.Size)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return 0, err
|
|
}
|
|
} else {
|
|
err := localfiles.PutObject(viper.GetString("localfs"), blob, response.Body)
|
|
if err != nil {
|
|
log.Printf("Could not create File object: %v \n", err)
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
|
|
// go RunStaticAnalysis(id)
|
|
return 0, nil
|
|
}
|