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 return err
} }
uploadImages(missingFiles) err = uploadImages(context, missingFiles, existingCategories)
if err != nil {
return err
}
return errors.New("synchronizeImages: NOT IMPLEMENTED") return errors.New("synchronizeImages: NOT IMPLEMENTED")
} }
@ -53,6 +56,19 @@ func findMissingImages(context *appContext, imageFiles []*localFileStructure.Ima
return missingFiles, nil 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)") 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

@ -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) logrus.Debugf("Local Image %s - %s - %s", md5sum, file.ModTime.Format(time.RFC3339), file.Path)
imageFiles = append(imageFiles, &ImageNode{ imageFiles = append(imageFiles, &ImageNode{
Path: file.Path, Path: file.Path,
Directory: filepath.Dir(file.Path), CategoryName: filepath.Dir(file.Key),
ModTime: file.ModTime, ModTime: file.ModTime,
Md5Sum: md5sum, Md5Sum: md5sum,
}) })
} }

View File

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

View File

@ -5,6 +5,11 @@ type uploadChunkResponse struct {
Result interface{} `json:"result"` Result interface{} `json:"result"`
} }
type fileAddResponse struct {
Status string `json:"stat"`
Result string `json:"result"`
}
type imageExistResponse struct { type imageExistResponse struct {
Stat string `json:"stat"` Stat string `json:"stat"`
Result map[string]string `json:"result"` Result map[string]string `json:"result"`

View File

@ -1,10 +1,95 @@
package picture package picture
/* import (
http://pictures.haefelfinger.net/ws.php?format=json "encoding/json"
{ "errors"
"data": "123456", "fmt"
"original_sum": "1234567", "github.com/sirupsen/logrus"
"position": "1" "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
} }
*/