small http handling optimization
This commit is contained in:
parent
28f89c54d5
commit
704a3da759
@ -34,7 +34,7 @@ func findMissingImages(context *appContext, imageFiles []*localFileStructure.Ima
|
|||||||
|
|
||||||
files := make([]string, 0, len(imageFiles))
|
files := make([]string, 0, len(imageFiles))
|
||||||
md5map := make(map[string]*localFileStructure.ImageNode, len(imageFiles))
|
md5map := make(map[string]*localFileStructure.ImageNode, len(imageFiles))
|
||||||
for _, file := range imageFiles {
|
for _, file := range imageFiles {
|
||||||
md5map[file.Md5Sum] = file
|
md5map[file.Md5Sum] = file
|
||||||
files = append(files, file.Md5Sum)
|
files = append(files, file.Md5Sum)
|
||||||
}
|
}
|
||||||
@ -45,7 +45,7 @@ func findMissingImages(context *appContext, imageFiles []*localFileStructure.Ima
|
|||||||
}
|
}
|
||||||
|
|
||||||
missingFiles := make([]*localFileStructure.ImageNode, 0, len(misingSums))
|
missingFiles := make([]*localFileStructure.ImageNode, 0, len(misingSums))
|
||||||
for _, sum := range misingSums {
|
for _, sum := range misingSums {
|
||||||
file := md5map[sum]
|
file := md5map[sum]
|
||||||
logrus.Infof("Found missing file %s", file.Path)
|
logrus.Infof("Found missing file %s", file.Path)
|
||||||
missingFiles = append(missingFiles, file)
|
missingFiles = append(missingFiles, file)
|
||||||
|
37
internal/pkg/piwigo/PiwigoContext.go
Normal file
37
internal/pkg/piwigo/PiwigoContext.go
Normal 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
|
||||||
|
}
|
@ -6,8 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
||||||
"net/http"
|
|
||||||
"net/http/cookiejar"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"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)
|
logrus.Warnf("The server url %s does not use https! Credentials are not encrypted!", context.Url)
|
||||||
}
|
}
|
||||||
|
|
||||||
initializeCookieJarIfRequired(context)
|
|
||||||
|
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
formData.Set("method", "pwg.session.login")
|
formData.Set("method", "pwg.session.login")
|
||||||
formData.Set("username", context.Username)
|
formData.Set("username", context.Username)
|
||||||
formData.Set("password", context.Password)
|
formData.Set("password", context.Password)
|
||||||
|
|
||||||
client := http.Client{Jar: context.Cookies}
|
response, err := context.PostForm(formData)
|
||||||
|
|
||||||
response, err := client.PostForm(context.Url, formData)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("The HTTP request failed with error %s", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
@ -54,15 +47,11 @@ func Login(context *piwigo.PiwigoContext) error {
|
|||||||
func Logout(context *piwigo.PiwigoContext) error {
|
func Logout(context *piwigo.PiwigoContext) error {
|
||||||
logrus.Debugf("Logging out from %s", context.Url)
|
logrus.Debugf("Logging out from %s", context.Url)
|
||||||
|
|
||||||
initializeCookieJarIfRequired(context)
|
|
||||||
|
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
formData.Set("method", "pwg.session.logout")
|
formData.Set("method", "pwg.session.logout")
|
||||||
|
|
||||||
client := http.Client{Jar: context.Cookies}
|
response, err := context.PostForm(formData)
|
||||||
response, err := client.PostForm(context.Url, formData)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
@ -82,18 +71,13 @@ func Logout(context *piwigo.PiwigoContext) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetStatus(context *piwigo.PiwigoContext) (*GetStatusResponse, error) {
|
func GetStatus(context *piwigo.PiwigoContext) (*GetStatusResponse, error) {
|
||||||
|
|
||||||
logrus.Debugln("Getting current login state...")
|
logrus.Debugln("Getting current login state...")
|
||||||
|
|
||||||
initializeCookieJarIfRequired(context)
|
|
||||||
|
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
formData.Set("method", "pwg.session.getStatus")
|
formData.Set("method", "pwg.session.getStatus")
|
||||||
|
|
||||||
client := http.Client{Jar: context.Cookies}
|
response, err := context.PostForm(formData)
|
||||||
response, err := client.PostForm(context.Url, formData)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s\n", err)
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
@ -112,13 +96,3 @@ func GetStatus(context *piwigo.PiwigoContext) (*GetStatusResponse, error) {
|
|||||||
|
|
||||||
return &statusResponse, nil
|
return &statusResponse, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializeCookieJarIfRequired(context *piwigo.PiwigoContext) {
|
|
||||||
if context.Cookies != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
options := cookiejar.Options{}
|
|
||||||
jar, _ := cookiejar.New(&options)
|
|
||||||
context.Cookies = jar
|
|
||||||
}
|
|
||||||
|
@ -6,15 +6,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateCategory(context *piwigo.PiwigoContext, parentId int, name string) (int, error) {
|
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 := url.Values{}
|
||||||
formData.Set("method", "pwg.categories.add")
|
formData.Set("method", "pwg.categories.add")
|
||||||
formData.Set("name", name)
|
formData.Set("name", name)
|
||||||
@ -24,10 +19,8 @@ func CreateCategory(context *piwigo.PiwigoContext, parentId int, name string) (i
|
|||||||
formData.Set("parent", fmt.Sprint(parentId))
|
formData.Set("parent", fmt.Sprint(parentId))
|
||||||
}
|
}
|
||||||
|
|
||||||
client := http.Client{Jar: context.Cookies}
|
response, err := context.PostForm(formData)
|
||||||
response, err := client.PostForm(context.Url, formData)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s", err)
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
@ -6,24 +6,17 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetAllCategories(context *piwigo.PiwigoContext) (map[string]*PiwigoCategory, error) {
|
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 := url.Values{}
|
||||||
formData.Set("method", "pwg.categories.getList")
|
formData.Set("method", "pwg.categories.getList")
|
||||||
formData.Set("recursive", "true")
|
formData.Set("recursive", "true")
|
||||||
|
|
||||||
client := http.Client{Jar: context.Cookies}
|
response, err := context.PostForm(formData)
|
||||||
response, err := client.PostForm(context.Url, formData)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s", err)
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
@ -39,7 +32,7 @@ func GetAllCategories(context *piwigo.PiwigoContext) (map[string]*PiwigoCategory
|
|||||||
return nil, errors.New("Could not load categories")
|
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)
|
categories := buildCategoryMap(&statusResponse)
|
||||||
buildCategoryKeys(categories)
|
buildCategoryKeys(categories)
|
||||||
|
@ -2,20 +2,13 @@ package picture
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ImageUploadRequired(context *piwigo.PiwigoContext, md5sums []string) ([]string, error) {
|
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
|
//TODO: make sure to split to multiple queries -> to honor max upload queries
|
||||||
|
|
||||||
md5sumList := strings.Join(md5sums, ",")
|
md5sumList := strings.Join(md5sums, ",")
|
||||||
@ -26,10 +19,8 @@ func ImageUploadRequired(context *piwigo.PiwigoContext, md5sums []string) ([]str
|
|||||||
|
|
||||||
logrus.Tracef("Looking up missing files: %s", md5sumList)
|
logrus.Tracef("Looking up missing files: %s", md5sumList)
|
||||||
|
|
||||||
client := http.Client{Jar: context.Cookies}
|
response, err := context.PostForm(formData)
|
||||||
response, err := client.PostForm(context.Url, formData)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s", err)
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
@ -6,29 +6,20 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func UploadImage(context *piwigo.PiwigoContext, filePath string, md5sum string, category int) (int, error) {
|
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)
|
logrus.Infof("Uploading %s", filePath)
|
||||||
|
|
||||||
// split into chunks
|
// split into chunks
|
||||||
// upload chunks
|
// upload chunks
|
||||||
// finalize upload
|
// finalize upload
|
||||||
|
|
||||||
|
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func uploadImageChunk(context *piwigo.PiwigoContext, base64chunk string, md5sum string, position int) error {
|
func uploadImageChunk(context *piwigo.PiwigoContext, base64chunk string, md5sum string, position int) error {
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
formData.Set("method", "pwg.images.addChunk")
|
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)
|
logrus.Tracef("Uploading chunk %d of file with sum %s", position, md5sum)
|
||||||
|
|
||||||
client := http.Client{Jar: context.Cookies}
|
response, err := context.PostForm(formData)
|
||||||
response, err := client.PostForm(context.Url, formData)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
@ -62,7 +51,6 @@ func uploadImageChunk(context *piwigo.PiwigoContext, base64chunk string, md5sum
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func uploadImageFinal(context *piwigo.PiwigoContext, originalFilename string, md5sum string, categoryId int) error {
|
func uploadImageFinal(context *piwigo.PiwigoContext, originalFilename string, md5sum string, categoryId int) error {
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
formData.Set("method", "pwg.images.add")
|
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)
|
logrus.Debugf("Finalizing upload of file %s with sum %s to category %d", originalFilename, md5sum, categoryId)
|
||||||
|
|
||||||
client := http.Client{Jar: context.Cookies}
|
response, err := context.PostForm(formData)
|
||||||
response, err := client.PostForm(context.Url, formData)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
package piwigo
|
|
||||||
|
|
||||||
import "net/http/cookiejar"
|
|
||||||
|
|
||||||
type PiwigoContext struct {
|
|
||||||
Url string
|
|
||||||
Username string
|
|
||||||
Password string
|
|
||||||
Cookies *cookiejar.Jar
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user