Download cover when importing from inventaire
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
package fileutils
|
||||
|
||||
import (
|
||||
"io"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
@@ -24,6 +30,56 @@ func SaveStaticFile(ac *appcontext.AppContext, file *multipart.FileHeader) (mode
|
||||
return staticFile, nil
|
||||
}
|
||||
|
||||
func DownloadFile(ac appcontext.AppContext, fileUrl string) (model.StaticFile, error) {
|
||||
var staticFile model.StaticFile
|
||||
u, err := url.Parse(fileUrl)
|
||||
if err != nil {
|
||||
return staticFile, err
|
||||
}
|
||||
path := u.Path
|
||||
segments := strings.Split(path, "/")
|
||||
fileName := segments[len(segments)-1]
|
||||
|
||||
response, err := http.Get(fileUrl)
|
||||
if err != nil {
|
||||
return staticFile, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
//get file extension from http header if needed
|
||||
if filepath.Ext(fileName) == "" {
|
||||
contentType := response.Header.Get("content-type")
|
||||
if contentType != "" {
|
||||
extensions, err := mime.ExtensionsByType(contentType)
|
||||
if err != nil {
|
||||
return staticFile, err
|
||||
}
|
||||
if len(extensions) > 0 {
|
||||
fileName = fileName + extensions[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
filePath := computePathFromName(&ac, fileName)
|
||||
|
||||
file, err := os.Create(ac.Config.ImageFolderPath + "/" + filePath)
|
||||
if err != nil {
|
||||
return staticFile, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = io.Copy(file, response.Body)
|
||||
if err != nil {
|
||||
return staticFile, err
|
||||
}
|
||||
staticFile = model.StaticFile{
|
||||
Name: fileName,
|
||||
Path: filePath,
|
||||
}
|
||||
err = ac.Db.Save(&staticFile).Error
|
||||
return staticFile, err
|
||||
}
|
||||
|
||||
func computePathFromName(ac *appcontext.AppContext, filename string) string {
|
||||
var existingFiles []model.StaticFile
|
||||
ac.Db.Where("name = ?", filename).Find(&existingFiles)
|
||||
|
||||
@@ -5,10 +5,10 @@ import (
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/fileutils"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/inventaire"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/openlibrary"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -51,6 +51,14 @@ func saveInventaireBookToDb(ac appcontext.AppContext, inventaireEdition inventai
|
||||
Author: *author,
|
||||
AddedBy: *user,
|
||||
}
|
||||
|
||||
if inventaireEdition.Image != "" {
|
||||
cover, err := fileutils.DownloadFile(ac, inventaireEdition.Image)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
book.Cover = cover
|
||||
}
|
||||
err = ac.Db.Save(&book).Error
|
||||
return &book, err
|
||||
}
|
||||
@@ -74,33 +82,3 @@ func fetchOrCreateInventaireAuthor(ac appcontext.AppContext, inventaireAuthor *i
|
||||
return &author, nil
|
||||
}
|
||||
}
|
||||
|
||||
func createAuthorFromOpenLibrary(ac appcontext.AppContext, openlibraryAuthorId string) (*model.Author, error) {
|
||||
authorFromOL, err := openlibrary.CallOpenLibraryAuthor(openlibraryAuthorId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var author model.Author
|
||||
res := ac.Db.Where("name = ?", authorFromOL.Name).First(&author)
|
||||
err = res.Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
author = model.Author{
|
||||
Name: authorFromOL.Name,
|
||||
Description: authorFromOL.Description,
|
||||
OpenLibraryId: openlibraryAuthorId,
|
||||
}
|
||||
err = ac.Db.Save(&author).Error
|
||||
return &author, err
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
//if the author already exists, only fill the open library id
|
||||
author.OpenLibraryId = openlibraryAuthorId
|
||||
ac.Db.Save(&author)
|
||||
}
|
||||
|
||||
return &author, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user