small http handling optimization

This commit is contained in:
Philipp Häfelfinger 2019-03-01 00:26:15 +01:00
parent 28f89c54d5
commit 704a3da759
8 changed files with 48 additions and 84 deletions

View File

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

View File

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

View File

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

View File

@ -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()

View File

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

View File

@ -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()

View File

@ -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()

View File

@ -1,10 +0,0 @@
package piwigo
import "net/http/cookiejar"
type PiwigoContext struct {
Url string
Username string
Password string
Cookies *cookiejar.Jar
}