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

@@ -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
}