diff --git a/internal/app/images.go b/internal/app/images.go index a612410..4b13655 100644 --- a/internal/app/images.go +++ b/internal/app/images.go @@ -2,8 +2,8 @@ package app import ( "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure" + "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo/category" - "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo/picture" "github.com/sirupsen/logrus" "sort" ) @@ -41,7 +41,7 @@ func findMissingImages(context *appContext, imageFiles []*localFileStructure.Ima files = append(files, file.Md5Sum) } - misingSums, err := picture.ImageUploadRequired(context.Piwigo, files) + misingSums, err := piwigo.ImageUploadRequired(context.Piwigo, files) if err != nil { return nil, err } @@ -68,7 +68,7 @@ func uploadImages(context *appContext, missingFiles []*localFileStructure.ImageN for _, file := range missingFiles { categoryId := existingCategories[file.CategoryName].Id - imageId, err := picture.UploadImage(context.Piwigo, file.Path, file.Md5Sum, categoryId) + imageId, err := piwigo.UploadImage(context.Piwigo, file.Path, file.Md5Sum, categoryId) if err != nil { return err } diff --git a/internal/pkg/piwigo/picture/upload.go b/internal/pkg/piwigo/picture.go similarity index 64% rename from internal/pkg/piwigo/picture/upload.go rename to internal/pkg/piwigo/picture.go index 0517a55..c0b27e2 100644 --- a/internal/pkg/piwigo/picture/upload.go +++ b/internal/pkg/piwigo/picture.go @@ -1,4 +1,4 @@ -package picture +package piwigo import ( "bufio" @@ -6,15 +6,69 @@ import ( "encoding/json" "errors" "fmt" - "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo" "github.com/sirupsen/logrus" "io" "net/url" "os" "strconv" + "strings" ) -func UploadImage(context *piwigo.PiwigoContext, filePath string, md5sum string, category int) (int, error) { +type uploadChunkResponse struct { + Status string `json:"stat"` + Result interface{} `json:"result"` +} + +type fileAddResponse struct { + Status string `json:"stat"` + Result struct { + ImageID int `json:"image_id"` + URL string `json:"url"` + } `json:"result"` +} + +type imageExistResponse struct { + Stat string `json:"stat"` + Result map[string]string `json:"result"` +} + +func ImageUploadRequired(context *PiwigoContext, md5sums []string) ([]string, error) { + //TODO: make sure to split to multiple queries -> to honor max upload queries + //TODO: Make sure to return the found imageIds of the found sums to update the local image nodes + + md5sumList := strings.Join(md5sums, ",") + + formData := url.Values{} + formData.Set("method", "pwg.images.exist") + formData.Set("md5sum_list", md5sumList) + + logrus.Tracef("Looking up missing files: %s", md5sumList) + + response, err := context.PostForm(formData) + if err != nil { + return nil, err + } + defer response.Body.Close() + + var imageExistResponse imageExistResponse + if err := json.NewDecoder(response.Body).Decode(&imageExistResponse); err != nil { + logrus.Errorln(err) + return nil, err + } + + missingFiles := make([]string, 0, len(imageExistResponse.Result)) + + for key, value := range imageExistResponse.Result { + if value == "" { + logrus.Tracef("Missing file with md5sum: %s", key) + missingFiles = append(missingFiles, key) + } + } + + return missingFiles, nil +} + +func UploadImage(context *PiwigoContext, filePath string, md5sum string, category int) (int, error) { if context.ChunkSizeInKB <= 0 { return 0, errors.New("Uploadchunk size is less or equal to zero. 512 is a recommendet value to begin with.") } @@ -40,7 +94,7 @@ func UploadImage(context *piwigo.PiwigoContext, filePath string, md5sum string, return imageId, nil } -func uploadImageChunks(filePath string, context *piwigo.PiwigoContext, fileSizeInKB int64, md5sum string) error { +func uploadImageChunks(filePath string, context *PiwigoContext, fileSizeInKB int64, md5sum string) error { file, err := os.Open(filePath) if err != nil { return err @@ -76,7 +130,7 @@ func uploadImageChunks(filePath string, context *piwigo.PiwigoContext, fileSizeI return nil } -func uploadImageChunk(context *piwigo.PiwigoContext, base64chunk string, md5sum string, position int64) error { +func uploadImageChunk(context *PiwigoContext, base64chunk string, md5sum string, position int64) error { formData := url.Values{} formData.Set("method", "pwg.images.addChunk") formData.Set("data", base64chunk) @@ -107,7 +161,7 @@ func uploadImageChunk(context *piwigo.PiwigoContext, base64chunk string, md5sum return nil } -func uploadImageFinal(context *piwigo.PiwigoContext, originalFilename string, md5sum string, categoryId int) (int, error) { +func uploadImageFinal(context *PiwigoContext, originalFilename string, md5sum string, categoryId int) (int, error) { formData := url.Values{} formData.Set("method", "pwg.images.add") formData.Set("original_sum", md5sum) diff --git a/internal/pkg/piwigo/picture/query.go b/internal/pkg/piwigo/picture/query.go deleted file mode 100644 index 330b4a6..0000000 --- a/internal/pkg/piwigo/picture/query.go +++ /dev/null @@ -1,45 +0,0 @@ -package picture - -import ( - "encoding/json" - "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo" - "github.com/sirupsen/logrus" - "net/url" - "strings" -) - -func ImageUploadRequired(context *piwigo.PiwigoContext, md5sums []string) ([]string, error) { - //TODO: make sure to split to multiple queries -> to honor max upload queries - //TODO: Make sure to return the found imageIds of the found sums to update the local image nodes - - md5sumList := strings.Join(md5sums, ",") - - formData := url.Values{} - formData.Set("method", "pwg.images.exist") - formData.Set("md5sum_list", md5sumList) - - logrus.Tracef("Looking up missing files: %s", md5sumList) - - response, err := context.PostForm(formData) - if err != nil { - return nil, err - } - defer response.Body.Close() - - var imageExistResponse imageExistResponse - if err := json.NewDecoder(response.Body).Decode(&imageExistResponse); err != nil { - logrus.Errorln(err) - return nil, err - } - - missingFiles := make([]string, 0, len(imageExistResponse.Result)) - - for key, value := range imageExistResponse.Result { - if value == "" { - logrus.Tracef("Missing file with md5sum: %s", key) - missingFiles = append(missingFiles, key) - } - } - - return missingFiles, nil -} diff --git a/internal/pkg/piwigo/picture/types.go b/internal/pkg/piwigo/picture/types.go deleted file mode 100644 index 3a4ef51..0000000 --- a/internal/pkg/piwigo/picture/types.go +++ /dev/null @@ -1,19 +0,0 @@ -package picture - -type uploadChunkResponse struct { - Status string `json:"stat"` - Result interface{} `json:"result"` -} - -type fileAddResponse struct { - Status string `json:"stat"` - Result struct { - ImageID int `json:"image_id"` - URL string `json:"url"` - } `json:"result"` -} - -type imageExistResponse struct { - Stat string `json:"stat"` - Result map[string]string `json:"result"` -}