Download cover when importing from inventaire

This commit is contained in:
2026-02-03 18:05:42 +01:00
parent 82db737d30
commit 32e8e4d963
2 changed files with 65 additions and 31 deletions

View File

@@ -1,9 +1,15 @@
package fileutils package fileutils
import ( import (
"io"
"mime"
"mime/multipart" "mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"git.artlef.fr/PersonalLibraryManager/internal/appcontext" "git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/model" "git.artlef.fr/PersonalLibraryManager/internal/model"
@@ -24,6 +30,56 @@ func SaveStaticFile(ac *appcontext.AppContext, file *multipart.FileHeader) (mode
return staticFile, nil 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 { func computePathFromName(ac *appcontext.AppContext, filename string) string {
var existingFiles []model.StaticFile var existingFiles []model.StaticFile
ac.Db.Where("name = ?", filename).Find(&existingFiles) ac.Db.Where("name = ?", filename).Find(&existingFiles)

View File

@@ -5,10 +5,10 @@ import (
"git.artlef.fr/PersonalLibraryManager/internal/appcontext" "git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/dto" "git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/fileutils"
"git.artlef.fr/PersonalLibraryManager/internal/inventaire" "git.artlef.fr/PersonalLibraryManager/internal/inventaire"
"git.artlef.fr/PersonalLibraryManager/internal/model" "git.artlef.fr/PersonalLibraryManager/internal/model"
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator" "git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
"git.artlef.fr/PersonalLibraryManager/internal/openlibrary"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gorm.io/gorm" "gorm.io/gorm"
) )
@@ -51,6 +51,14 @@ func saveInventaireBookToDb(ac appcontext.AppContext, inventaireEdition inventai
Author: *author, Author: *author,
AddedBy: *user, 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 err = ac.Db.Save(&book).Error
return &book, err return &book, err
} }
@@ -74,33 +82,3 @@ func fetchOrCreateInventaireAuthor(ac appcontext.AppContext, inventaireAuthor *i
return &author, nil 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
}