renamed PiwigoContext to ServerContext
This commit is contained in:
parent
6bde70cef8
commit
3de900ab75
@ -14,7 +14,7 @@ import (
|
|||||||
|
|
||||||
type appContext struct {
|
type appContext struct {
|
||||||
// think again if this is a good idea to have such a context!
|
// think again if this is a good idea to have such a context!
|
||||||
piwigo *piwigo.PiwigoContext
|
piwigo *piwigo.ServerContext
|
||||||
dataStore *datastore.LocalDataStore
|
dataStore *datastore.LocalDataStore
|
||||||
sessionId string
|
sessionId string
|
||||||
localRootPath string
|
localRootPath string
|
||||||
@ -45,7 +45,7 @@ func (c *appContext) usePiwigo(url string, user string, password string) error {
|
|||||||
return errors.New("missing piwigo password")
|
return errors.New("missing piwigo password")
|
||||||
}
|
}
|
||||||
|
|
||||||
c.piwigo = new(piwigo.PiwigoContext)
|
c.piwigo = new(piwigo.ServerContext)
|
||||||
return c.piwigo.Initialize(url, user, password)
|
return c.piwigo.Initialize(url, user, password)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ const (
|
|||||||
ImageStateDifferent = 1
|
ImageStateDifferent = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
func uploadImageChunks(filePath string, context *PiwigoContext, fileSizeInKB int64, md5sum string) error {
|
func uploadImageChunks(filePath string, context *ServerContext, fileSizeInKB int64, md5sum string) error {
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -60,7 +60,7 @@ func uploadImageChunks(filePath string, context *PiwigoContext, fileSizeInKB int
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func uploadImageChunk(context *PiwigoContext, base64chunk string, md5sum string, position int64) error {
|
func uploadImageChunk(context *ServerContext, base64chunk string, md5sum string, position int64) error {
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
formData.Set("method", "pwg.images.addChunk")
|
formData.Set("method", "pwg.images.addChunk")
|
||||||
formData.Set("data", base64chunk)
|
formData.Set("data", base64chunk)
|
||||||
@ -81,7 +81,7 @@ func uploadImageChunk(context *PiwigoContext, base64chunk string, md5sum string,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func uploadImageFinal(context *PiwigoContext, piwigoId int, originalFilename string, md5sum string, categoryId int) (int, error) {
|
func uploadImageFinal(context *ServerContext, piwigoId int, originalFilename string, md5sum string, categoryId int) (int, error) {
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
formData.Set("method", "pwg.images.add")
|
formData.Set("method", "pwg.images.add")
|
||||||
formData.Set("original_sum", md5sum)
|
formData.Set("original_sum", md5sum)
|
||||||
|
@ -30,7 +30,7 @@ type PiwigoImageApi interface {
|
|||||||
DeleteImages(imageIds []int) error
|
DeleteImages(imageIds []int) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type PiwigoContext struct {
|
type ServerContext struct {
|
||||||
url string
|
url string
|
||||||
username string
|
username string
|
||||||
password string
|
password string
|
||||||
@ -38,7 +38,7 @@ type PiwigoContext struct {
|
|||||||
cookies *cookiejar.Jar
|
cookies *cookiejar.Jar
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) Initialize(baseUrl string, username string, password string) error {
|
func (context *ServerContext) Initialize(baseUrl string, username string, password string) error {
|
||||||
if baseUrl == "" {
|
if baseUrl == "" {
|
||||||
return errors.New("please provide a valid piwigo server base URL")
|
return errors.New("please provide a valid piwigo server base URL")
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ func (context *PiwigoContext) Initialize(baseUrl string, username string, passwo
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) Login() error {
|
func (context *ServerContext) Login() error {
|
||||||
logrus.Infoln("Logging in to piwigo and getting chunk size configuration for uploads")
|
logrus.Infoln("Logging in to piwigo and getting chunk size configuration for uploads")
|
||||||
logrus.Debugf("Logging in to %s using user %s", context.url, context.username)
|
logrus.Debugf("Logging in to %s using user %s", context.url, context.username)
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ func (context *PiwigoContext) Login() error {
|
|||||||
return context.initializeUploadChunkSize()
|
return context.initializeUploadChunkSize()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) Logout() error {
|
func (context *ServerContext) Logout() error {
|
||||||
logrus.Debugf("Logging out from %s", context.url)
|
logrus.Debugf("Logging out from %s", context.url)
|
||||||
|
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
@ -101,7 +101,7 @@ func (context *PiwigoContext) Logout() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) getStatus() (*getStatusResponse, error) {
|
func (context *ServerContext) getStatus() (*getStatusResponse, error) {
|
||||||
logrus.Debugln("Getting current login state...")
|
logrus.Debugln("Getting current login state...")
|
||||||
|
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
@ -118,7 +118,7 @@ func (context *PiwigoContext) getStatus() (*getStatusResponse, error) {
|
|||||||
return &response, nil
|
return &response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) GetAllCategories() (map[string]*Category, error) {
|
func (context *ServerContext) GetAllCategories() (map[string]*Category, error) {
|
||||||
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")
|
||||||
@ -138,7 +138,7 @@ func (context *PiwigoContext) GetAllCategories() (map[string]*Category, error) {
|
|||||||
return categoryLookups, nil
|
return categoryLookups, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) CreateCategory(parentId int, name string) (int, error) {
|
func (context *ServerContext) CreateCategory(parentId int, name string) (int, error) {
|
||||||
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)
|
||||||
@ -159,7 +159,7 @@ func (context *PiwigoContext) CreateCategory(parentId int, name string) (int, er
|
|||||||
return response.Result.ID, nil
|
return response.Result.ID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) ImageCheckFile(piwigoId int, md5sum string) (int, error) {
|
func (context *ServerContext) ImageCheckFile(piwigoId int, md5sum string) (int, error) {
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
formData.Set("method", "pwg.images.checkFiles")
|
formData.Set("method", "pwg.images.checkFiles")
|
||||||
formData.Set("image_id", strconv.Itoa(piwigoId))
|
formData.Set("image_id", strconv.Itoa(piwigoId))
|
||||||
@ -179,7 +179,7 @@ func (context *PiwigoContext) ImageCheckFile(piwigoId int, md5sum string) (int,
|
|||||||
return ImageStateDifferent, nil
|
return ImageStateDifferent, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) ImagesExistOnPiwigo(md5sums []string) (map[string]int, error) {
|
func (context *ServerContext) ImagesExistOnPiwigo(md5sums []string) (map[string]int, error) {
|
||||||
existResults := make(map[string]int, len(md5sums))
|
existResults := make(map[string]int, len(md5sums))
|
||||||
|
|
||||||
batchSize := 2000
|
batchSize := 2000
|
||||||
@ -197,7 +197,7 @@ func (context *PiwigoContext) ImagesExistOnPiwigo(md5sums []string) (map[string]
|
|||||||
return existResults, nil
|
return existResults, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) imagesExistOnPiwigoBatch(md5sums []string, existResults map[string]int) error {
|
func (context *ServerContext) imagesExistOnPiwigoBatch(md5sums []string, existResults map[string]int) error {
|
||||||
md5sumList := strings.Join(md5sums, "|")
|
md5sumList := strings.Join(md5sums, "|")
|
||||||
|
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
@ -230,7 +230,7 @@ func (context *PiwigoContext) imagesExistOnPiwigoBatch(md5sums []string, existRe
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) UploadImage(piwigoId int, filePath string, md5sum string, category int) (int, error) {
|
func (context *ServerContext) UploadImage(piwigoId int, filePath string, md5sum string, category int) (int, error) {
|
||||||
if context.chunkSizeInKB <= 0 {
|
if context.chunkSizeInKB <= 0 {
|
||||||
return 0, errors.New("uploadchunk size is less or equal to zero. 512 is a recommendet value to begin with")
|
return 0, errors.New("uploadchunk size is less or equal to zero. 512 is a recommendet value to begin with")
|
||||||
}
|
}
|
||||||
@ -256,7 +256,7 @@ func (context *PiwigoContext) UploadImage(piwigoId int, filePath string, md5sum
|
|||||||
return imageId, nil
|
return imageId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) DeleteImages(imageIds []int) error {
|
func (context *ServerContext) DeleteImages(imageIds []int) error {
|
||||||
logrus.Debug("Entering DeleteImages")
|
logrus.Debug("Entering DeleteImages")
|
||||||
defer logrus.Debug("Leaving DeleteImages")
|
defer logrus.Debug("Leaving DeleteImages")
|
||||||
|
|
||||||
@ -282,7 +282,7 @@ func (context *PiwigoContext) DeleteImages(imageIds []int) error {
|
|||||||
return context.executePiwigoRequest(formData, &response)
|
return context.executePiwigoRequest(formData, &response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) getPiwigoToken() (string, error) {
|
func (context *ServerContext) getPiwigoToken() (string, error) {
|
||||||
logrus.Debug("Entering getPiwigoToken")
|
logrus.Debug("Entering getPiwigoToken")
|
||||||
defer logrus.Debug("Leaving getPiwigoToken")
|
defer logrus.Debug("Leaving getPiwigoToken")
|
||||||
|
|
||||||
@ -298,7 +298,7 @@ func (context *PiwigoContext) getPiwigoToken() (string, error) {
|
|||||||
return pwgToken, nil
|
return pwgToken, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) initializeCookieJarIfRequired() {
|
func (context *ServerContext) initializeCookieJarIfRequired() {
|
||||||
if context.cookies != nil {
|
if context.cookies != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -308,7 +308,7 @@ func (context *PiwigoContext) initializeCookieJarIfRequired() {
|
|||||||
context.cookies = jar
|
context.cookies = jar
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) initializeUploadChunkSize() error {
|
func (context *ServerContext) initializeUploadChunkSize() error {
|
||||||
userStatus, err := context.getStatus()
|
userStatus, err := context.getStatus()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -318,7 +318,7 @@ func (context *PiwigoContext) initializeUploadChunkSize() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *PiwigoContext) executePiwigoRequest(formData url.Values, decodedResponse responseStatuser) error {
|
func (context *ServerContext) executePiwigoRequest(formData url.Values, decodedResponse responseStatuser) error {
|
||||||
context.initializeCookieJarIfRequired()
|
context.initializeCookieJarIfRequired()
|
||||||
|
|
||||||
client := http.Client{Jar: context.cookies}
|
client := http.Client{Jar: context.cookies}
|
||||||
|
Loading…
Reference in New Issue
Block a user