started upload file implementation

This commit is contained in:
Philipp Häfelfinger 2019-03-01 00:14:52 +01:00
parent 958631df5d
commit 28f89c54d5
5 changed files with 123 additions and 17 deletions

View File

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

View File

@ -23,7 +23,7 @@ func GetImageList(fileSystem map[string]*FilesystemNode) ([]*ImageNode, error) {
imageFiles = append(imageFiles, &ImageNode{
Path: file.Path,
Directory: filepath.Dir(file.Path),
CategoryName: filepath.Dir(file.Key),
ModTime: file.ModTime,
Md5Sum: md5sum,
})

View File

@ -19,7 +19,7 @@ func (n *FilesystemNode) String() string {
type ImageNode struct {
Path string
Directory string
CategoryName string
ModTime time.Time
Md5Sum string
}

View File

@ -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"`

View File

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