From 704a3da7598ded43a4f73cb5fdf564c7517392e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Fri, 1 Mar 2019 00:26:15 +0100 Subject: [PATCH] small http handling optimization --- internal/app/images.go | 4 +- internal/pkg/piwigo/PiwigoContext.go | 37 +++++++++++++++++++ .../piwigo/authentication/authentication.go | 32 ++-------------- internal/pkg/piwigo/category/create.go | 9 +---- internal/pkg/piwigo/category/query.go | 11 +----- internal/pkg/piwigo/picture/query.go | 11 +----- internal/pkg/piwigo/picture/upload.go | 18 +-------- internal/pkg/piwigo/types.go | 10 ----- 8 files changed, 48 insertions(+), 84 deletions(-) create mode 100644 internal/pkg/piwigo/PiwigoContext.go delete mode 100644 internal/pkg/piwigo/types.go diff --git a/internal/app/images.go b/internal/app/images.go index ad37aaf..a20f22c 100644 --- a/internal/app/images.go +++ b/internal/app/images.go @@ -34,7 +34,7 @@ func findMissingImages(context *appContext, imageFiles []*localFileStructure.Ima files := make([]string, 0, len(imageFiles)) md5map := make(map[string]*localFileStructure.ImageNode, len(imageFiles)) - for _, file := range imageFiles { + for _, file := range imageFiles { md5map[file.Md5Sum] = file files = append(files, file.Md5Sum) } @@ -45,7 +45,7 @@ func findMissingImages(context *appContext, imageFiles []*localFileStructure.Ima } missingFiles := make([]*localFileStructure.ImageNode, 0, len(misingSums)) - for _, sum := range misingSums { + for _, sum := range misingSums { file := md5map[sum] logrus.Infof("Found missing file %s", file.Path) missingFiles = append(missingFiles, file) diff --git a/internal/pkg/piwigo/PiwigoContext.go b/internal/pkg/piwigo/PiwigoContext.go new file mode 100644 index 0000000..d7f4c7b --- /dev/null +++ b/internal/pkg/piwigo/PiwigoContext.go @@ -0,0 +1,37 @@ +package piwigo + +import ( + "github.com/sirupsen/logrus" + "net/http" + "net/http/cookiejar" + "net/url" +) + +type PiwigoContext struct { + Url string + Username string + Password string + Cookies *cookiejar.Jar +} + +func (context *PiwigoContext) PostForm(formData url.Values) (resp *http.Response, err error) { + context.initializeCookieJarIfRequired() + + 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 nil, err + } + return response, nil +} + +func (context *PiwigoContext) initializeCookieJarIfRequired() { + if context.Cookies != nil { + return + } + + options := cookiejar.Options{} + jar, _ := cookiejar.New(&options) + context.Cookies = jar +} diff --git a/internal/pkg/piwigo/authentication/authentication.go b/internal/pkg/piwigo/authentication/authentication.go index ae613e5..2a98c0c 100644 --- a/internal/pkg/piwigo/authentication/authentication.go +++ b/internal/pkg/piwigo/authentication/authentication.go @@ -6,8 +6,6 @@ import ( "fmt" "github.com/sirupsen/logrus" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo" - "net/http" - "net/http/cookiejar" "net/url" "strings" ) @@ -19,18 +17,13 @@ func Login(context *piwigo.PiwigoContext) error { logrus.Warnf("The server url %s does not use https! Credentials are not encrypted!", context.Url) } - initializeCookieJarIfRequired(context) - formData := url.Values{} formData.Set("method", "pwg.session.login") formData.Set("username", context.Username) formData.Set("password", context.Password) - client := http.Client{Jar: context.Cookies} - - response, err := client.PostForm(context.Url, formData) + response, err := context.PostForm(formData) if err != nil { - logrus.Errorf("The HTTP request failed with error %s", err) return err } defer response.Body.Close() @@ -54,15 +47,11 @@ func Login(context *piwigo.PiwigoContext) error { func Logout(context *piwigo.PiwigoContext) error { logrus.Debugf("Logging out from %s", context.Url) - initializeCookieJarIfRequired(context) - formData := url.Values{} formData.Set("method", "pwg.session.logout") - client := http.Client{Jar: context.Cookies} - response, err := client.PostForm(context.Url, formData) + response, err := context.PostForm(formData) if err != nil { - logrus.Errorln("The HTTP request failed with error %s", err) return err } defer response.Body.Close() @@ -82,18 +71,13 @@ func Logout(context *piwigo.PiwigoContext) error { } func GetStatus(context *piwigo.PiwigoContext) (*GetStatusResponse, error) { - logrus.Debugln("Getting current login state...") - initializeCookieJarIfRequired(context) - formData := url.Values{} formData.Set("method", "pwg.session.getStatus") - client := http.Client{Jar: context.Cookies} - response, err := client.PostForm(context.Url, formData) + response, err := context.PostForm(formData) if err != nil { - logrus.Errorln("The HTTP request failed with error %s\n", err) return nil, err } defer response.Body.Close() @@ -112,13 +96,3 @@ func GetStatus(context *piwigo.PiwigoContext) (*GetStatusResponse, error) { return &statusResponse, nil } - -func initializeCookieJarIfRequired(context *piwigo.PiwigoContext) { - if context.Cookies != nil { - return - } - - options := cookiejar.Options{} - jar, _ := cookiejar.New(&options) - context.Cookies = jar -} diff --git a/internal/pkg/piwigo/category/create.go b/internal/pkg/piwigo/category/create.go index 9ac6041..59febf4 100644 --- a/internal/pkg/piwigo/category/create.go +++ b/internal/pkg/piwigo/category/create.go @@ -6,15 +6,10 @@ import ( "fmt" "github.com/sirupsen/logrus" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo" - "net/http" "net/url" ) func CreateCategory(context *piwigo.PiwigoContext, parentId int, name string) (int, error) { - if context.Cookies == nil { - return 0, errors.New("Not logged in and no cookies found! Can not get the category list!") - } - formData := url.Values{} formData.Set("method", "pwg.categories.add") formData.Set("name", name) @@ -24,10 +19,8 @@ func CreateCategory(context *piwigo.PiwigoContext, parentId int, name string) (i formData.Set("parent", fmt.Sprint(parentId)) } - client := http.Client{Jar: context.Cookies} - response, err := client.PostForm(context.Url, formData) + response, err := context.PostForm(formData) if err != nil { - logrus.Errorln("The HTTP request failed with error %s", err) return 0, err } defer response.Body.Close() diff --git a/internal/pkg/piwigo/category/query.go b/internal/pkg/piwigo/category/query.go index 656412f..192441e 100644 --- a/internal/pkg/piwigo/category/query.go +++ b/internal/pkg/piwigo/category/query.go @@ -6,24 +6,17 @@ import ( "fmt" "github.com/sirupsen/logrus" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo" - "net/http" "net/url" "os" ) func GetAllCategories(context *piwigo.PiwigoContext) (map[string]*PiwigoCategory, error) { - if context.Cookies == nil { - return nil, errors.New("Not logged in and no cookies found! Can not get the category list!") - } - formData := url.Values{} formData.Set("method", "pwg.categories.getList") formData.Set("recursive", "true") - client := http.Client{Jar: context.Cookies} - response, err := client.PostForm(context.Url, formData) + response, err := context.PostForm(formData) if err != nil { - logrus.Errorln("The HTTP request failed with error %s", err) return nil, err } defer response.Body.Close() @@ -39,7 +32,7 @@ func GetAllCategories(context *piwigo.PiwigoContext) (map[string]*PiwigoCategory return nil, errors.New("Could not load categories") } - logrus.Infof("Successfully got all categories from %s", context.Url) + logrus.Infof("Successfully got all categories") categories := buildCategoryMap(&statusResponse) buildCategoryKeys(categories) diff --git a/internal/pkg/piwigo/picture/query.go b/internal/pkg/piwigo/picture/query.go index 81706a5..caecee1 100644 --- a/internal/pkg/piwigo/picture/query.go +++ b/internal/pkg/piwigo/picture/query.go @@ -2,20 +2,13 @@ package picture import ( "encoding/json" - "errors" "github.com/sirupsen/logrus" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo" - "net/http" "net/url" "strings" ) func ImageUploadRequired(context *piwigo.PiwigoContext, md5sums []string) ([]string, error) { - - if context.Cookies == nil { - return nil, errors.New("Not logged in and no cookies found! Can not get the category list!") - } - //TODO: make sure to split to multiple queries -> to honor max upload queries md5sumList := strings.Join(md5sums, ",") @@ -26,10 +19,8 @@ func ImageUploadRequired(context *piwigo.PiwigoContext, md5sums []string) ([]str logrus.Tracef("Looking up missing files: %s", md5sumList) - client := http.Client{Jar: context.Cookies} - response, err := client.PostForm(context.Url, formData) + response, err := context.PostForm(formData) if err != nil { - logrus.Errorln("The HTTP request failed with error %s", err) return nil, err } defer response.Body.Close() diff --git a/internal/pkg/piwigo/picture/upload.go b/internal/pkg/piwigo/picture/upload.go index 506a39e..0b37f3e 100644 --- a/internal/pkg/piwigo/picture/upload.go +++ b/internal/pkg/piwigo/picture/upload.go @@ -6,29 +6,20 @@ import ( "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") @@ -40,10 +31,8 @@ func uploadImageChunk(context *piwigo.PiwigoContext, base64chunk string, md5sum 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) + response, err := context.PostForm(formData) if err != nil { - logrus.Errorln("The HTTP request failed with error %s", err) return err } defer response.Body.Close() @@ -62,7 +51,6 @@ func uploadImageChunk(context *piwigo.PiwigoContext, base64chunk string, md5sum return nil } - func uploadImageFinal(context *piwigo.PiwigoContext, originalFilename string, md5sum string, categoryId int) error { formData := url.Values{} formData.Set("method", "pwg.images.add") @@ -72,10 +60,8 @@ func uploadImageFinal(context *piwigo.PiwigoContext, originalFilename string, md 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) + response, err := context.PostForm(formData) if err != nil { - logrus.Errorln("The HTTP request failed with error %s", err) return err } defer response.Body.Close() diff --git a/internal/pkg/piwigo/types.go b/internal/pkg/piwigo/types.go deleted file mode 100644 index 106ae0a..0000000 --- a/internal/pkg/piwigo/types.go +++ /dev/null @@ -1,10 +0,0 @@ -package piwigo - -import "net/http/cookiejar" - -type PiwigoContext struct { - Url string - Username string - Password string - Cookies *cookiejar.Jar -}