diff --git a/internal/app/images.go b/internal/app/images.go index 4a39343..ad37aaf 100644 --- a/internal/app/images.go +++ b/internal/app/images.go @@ -20,7 +20,10 @@ func synchronizeImages(context *appContext, fileSystem map[string]*localFileStru return err } - uploadImages(missingFiles) + err = uploadImages(context, missingFiles, existingCategories) + if err != nil { + return err + } return errors.New("synchronizeImages: NOT IMPLEMENTED") } @@ -53,6 +56,19 @@ func findMissingImages(context *appContext, imageFiles []*localFileStructure.Ima return missingFiles, nil } -func uploadImages(missingFiles []*localFileStructure.ImageNode) { +func uploadImages(context *appContext, missingFiles []*localFileStructure.ImageNode, existingCategories map[string]*category.PiwigoCategory) error { logrus.Warnln("Uploading missing images (NotImplemented)") + + for _, file := range missingFiles { + logrus.Infof("Uploading %s", file.Path) + categoryId := existingCategories[file.CategoryName].Id + + //TODO handle added id + _, err := picture.UploadImage(context.Piwigo, file.Path, file.Md5Sum, categoryId) + if err != nil { + return err + } + } + + return nil } diff --git a/internal/pkg/localFileStructure/imageList.go b/internal/pkg/localFileStructure/imageList.go index 34c7eb3..72ae3e0 100644 --- a/internal/pkg/localFileStructure/imageList.go +++ b/internal/pkg/localFileStructure/imageList.go @@ -22,10 +22,10 @@ func GetImageList(fileSystem map[string]*FilesystemNode) ([]*ImageNode, error) { logrus.Debugf("Local Image %s - %s - %s", md5sum, file.ModTime.Format(time.RFC3339), file.Path) imageFiles = append(imageFiles, &ImageNode{ - Path: file.Path, - Directory: filepath.Dir(file.Path), - ModTime: file.ModTime, - Md5Sum: md5sum, + Path: file.Path, + CategoryName: filepath.Dir(file.Key), + ModTime: file.ModTime, + Md5Sum: md5sum, }) } diff --git a/internal/pkg/localFileStructure/types.go b/internal/pkg/localFileStructure/types.go index c422739..92b33a3 100644 --- a/internal/pkg/localFileStructure/types.go +++ b/internal/pkg/localFileStructure/types.go @@ -18,8 +18,8 @@ func (n *FilesystemNode) String() string { } type ImageNode struct { - Path string - Directory string - ModTime time.Time - Md5Sum string + Path string + CategoryName string + ModTime time.Time + Md5Sum string } diff --git a/internal/pkg/piwigo/picture/types.go b/internal/pkg/piwigo/picture/types.go index c51acd4..5edf04d 100644 --- a/internal/pkg/piwigo/picture/types.go +++ b/internal/pkg/piwigo/picture/types.go @@ -5,6 +5,11 @@ type uploadChunkResponse struct { Result interface{} `json:"result"` } +type fileAddResponse struct { + Status string `json:"stat"` + Result string `json:"result"` +} + type imageExistResponse struct { Stat string `json:"stat"` Result map[string]string `json:"result"` diff --git a/internal/pkg/piwigo/picture/upload.go b/internal/pkg/piwigo/picture/upload.go index 7a0ab99..506a39e 100644 --- a/internal/pkg/piwigo/picture/upload.go +++ b/internal/pkg/piwigo/picture/upload.go @@ -1,10 +1,95 @@ package picture -/* -http://pictures.haefelfinger.net/ws.php?format=json -{ - "data": "123456", - "original_sum": "1234567", - "position": "1" +import ( + "encoding/json" + "errors" + "fmt" + "github.com/sirupsen/logrus" + "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo" + "net/http" + "net/url" + "strconv" +) + +func UploadImage(context *piwigo.PiwigoContext, filePath string, md5sum string, category int) (int, error) { + + if context.Cookies == nil { + return 0, errors.New("Not logged in and no cookies found! Can not get the category list!") + } + + logrus.Infof("Uploading %s", filePath) + + // split into chunks + // upload chunks + // finalize upload + + + return 0, nil +} + + + +func uploadImageChunk(context *piwigo.PiwigoContext, base64chunk string, md5sum string, position int) error { + formData := url.Values{} + formData.Set("method", "pwg.images.addChunk") + formData.Set("data", base64chunk) + formData.Set("original_sum", md5sum) + // required by the API for compatibility + formData.Set("type", "file") + formData.Set("position", strconv.Itoa(position)) + + logrus.Tracef("Uploading chunk %d of file with sum %s", position, md5sum) + + client := http.Client{Jar: context.Cookies} + response, err := client.PostForm(context.Url, formData) + if err != nil { + logrus.Errorln("The HTTP request failed with error %s", err) + return err + } + defer response.Body.Close() + + var uploadChunkResponse uploadChunkResponse + if err := json.NewDecoder(response.Body).Decode(&uploadChunkResponse); err != nil { + logrus.Errorln(err) + return err + } + + if uploadChunkResponse.Status != "ok" { + logrus.Errorf("Got state %s while uploading chunk %d of %s", uploadChunkResponse.Status, position, md5sum) + return errors.New(fmt.Sprintf("Got state %s while uploading chunk %d of %s", uploadChunkResponse.Status, position, md5sum)) + } + + return nil +} + + +func uploadImageFinal(context *piwigo.PiwigoContext, originalFilename string, md5sum string, categoryId int) error { + formData := url.Values{} + formData.Set("method", "pwg.images.add") + formData.Set("original_sum", md5sum) + formData.Set("original_filename", originalFilename) + formData.Set("categoriesi", strconv.Itoa(categoryId)) + + logrus.Debugf("Finalizing upload of file %s with sum %s to category %d", originalFilename, md5sum, categoryId) + + client := http.Client{Jar: context.Cookies} + response, err := client.PostForm(context.Url, formData) + if err != nil { + logrus.Errorln("The HTTP request failed with error %s", err) + return err + } + defer response.Body.Close() + + var fileAddResponse fileAddResponse + if err := json.NewDecoder(response.Body).Decode(&fileAddResponse); err != nil { + logrus.Errorln(err) + return err + } + + if fileAddResponse.Status != "ok" { + logrus.Errorf("Got state %s while adding image %s", fileAddResponse.Status, originalFilename) + return errors.New(fmt.Sprintf("Got state %s while adding image %s", fileAddResponse.Status, originalFilename)) + } + + return nil } -*/