- moved XML helper function into helpers package;
This commit is contained in:
parent
3b155acf9e
commit
228ca14f33
100
ext/cloud.go
100
ext/cloud.go
|
|
@ -1,13 +1,9 @@
|
||||||
package ext
|
package ext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"ticket-pimp/domain"
|
d "ticket-pimp/domain"
|
||||||
"ticket-pimp/helpers"
|
"ticket-pimp/helpers"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -17,8 +13,8 @@ type Cloud struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ICloud interface {
|
type ICloud interface {
|
||||||
CreateFolder(name string) (*domain.Cloud, error)
|
CreateFolder(name string) (*d.Folder, error)
|
||||||
ShareToExternals(cloud *domain.Cloud) (*domain.Cloud, error)
|
ShareToExternals(cloud *d.Folder) (*d.Folder, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCloud(base, user, pass string) *Cloud {
|
func NewCloud(base, user, pass string) *Cloud {
|
||||||
|
|
@ -35,83 +31,71 @@ func NewCloud(base, user, pass string) *Cloud {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type MultistatusObj struct {
|
func (c *Cloud) CreateFolder(name string) (*d.Folder, error) {
|
||||||
XMLName xml.Name `xml:"multistatus"`
|
|
||||||
Multistatus struct {
|
|
||||||
XMLName xml.Name `xml:"response"`
|
|
||||||
Propstat struct {
|
|
||||||
XMLName xml.Name `xml:"propstat"`
|
|
||||||
Prop struct {
|
|
||||||
XMLName xml.Name `xml:"prop"`
|
|
||||||
FileID struct {
|
|
||||||
XMLName xml.Name `xml:"fileid"`
|
|
||||||
ID string `xml:",chardata"`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Cloud) CreateFolder(name string) (*domain.Cloud, error) {
|
|
||||||
rootDir := os.Getenv("ROOTDIR")
|
rootDir := os.Getenv("ROOTDIR")
|
||||||
|
user := os.Getenv("CLOUD_USER")
|
||||||
|
|
||||||
|
davPath := "/remote.php/dav/files/"
|
||||||
|
parentPath := "/apps/files/?dir="
|
||||||
|
|
||||||
name = helpers.GitNaming(name)
|
name = helpers.GitNaming(name)
|
||||||
|
|
||||||
cloud := domain.Cloud{
|
cloud := d.Folder{
|
||||||
FolderName: name,
|
Title: name,
|
||||||
FolderURL: "",
|
PrivateURL: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
requestPath := os.Getenv("HOMEPATH") + rootDir + name
|
requestPath := davPath + user + rootDir + name
|
||||||
cloud.FolderPath = os.Getenv("FOLDER_PATH") + rootDir + name
|
|
||||||
|
cloud.PathTo = parentPath + rootDir + name
|
||||||
|
|
||||||
resp, err := c.R().
|
resp, err := c.R().
|
||||||
Send("MKCOL", requestPath)
|
Send("MKCOL", requestPath)
|
||||||
|
|
||||||
if resp.IsSuccessState() {
|
if resp.IsSuccessState() {
|
||||||
|
// Set stupid URL to the d entity
|
||||||
|
cloud.PrivateURL = c.BaseURL + cloud.PathTo
|
||||||
|
|
||||||
cloud.FolderURL = c.BaseURL + cloud.FolderPath
|
// Try to set short URL to the d entity
|
||||||
|
if err = c.setPrivateURL(requestPath, &cloud); err != nil {
|
||||||
xmlFile, err := ioutil.ReadFile("./fileid.xml")
|
return &cloud, nil
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return nil, err // fix this return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, _ := c.R().
|
|
||||||
SetBody(xmlFile).
|
|
||||||
Send("PROPFIND", os.Getenv("HOMEPATH")+os.Getenv("ROOTDIR")+cloud.FolderName)
|
|
||||||
|
|
||||||
id, err := getFileIDFromRespBody(resp.Bytes())
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Print(err) // [ ] Если тут проблема - надо пытаться засетать полную ссылку
|
|
||||||
}
|
|
||||||
|
|
||||||
cloud.PrivateURL = os.Getenv("CLOUD_BASE_URL") + "/f/" + strconv.Itoa(id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &cloud, err
|
return &cloud, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFileIDFromRespBody(str []byte) (int, error) {
|
func (c *Cloud) setPrivateURL(requestPath string, cloud *d.Folder) error {
|
||||||
|
|
||||||
var multi MultistatusObj
|
payload := []byte(`<?xml version="1.0"?><a:propfind xmlns:a="DAV:" xmlns:oc="http://owncloud.org/ns"><a:prop><oc:fileid/></a:prop></a:propfind>`)
|
||||||
|
|
||||||
|
// Deprecated: Read XML file
|
||||||
|
/*
|
||||||
|
xmlFile, err := ioutil.ReadFile("./fileid.xml") // moved into this method as a string..
|
||||||
|
|
||||||
err := xml.Unmarshal(str, &multi)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("XML Unmarshal error: %v", err)
|
return fmt.Errorf("request xml file error: %v", err)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
resp, err := c.R().
|
||||||
|
SetBody(payload).
|
||||||
|
Send("PROPFIND", requestPath)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := strconv.Atoi(multi.Multistatus.Propstat.Prop.FileID.ID)
|
id := helpers.GetFileIDFromRespBody(resp.Bytes())
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("FileID str to int convertion error: %v", err)
|
if id != 0 {
|
||||||
|
cloud.PrivateURL = c.BaseURL + "/f/" + strconv.Itoa(id)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return id, nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cloud) ShareToExternals(cloud *domain.Cloud) (*domain.Cloud, error) {
|
func (c *Cloud) ShareToExternals(cloud *d.Folder) (*d.Folder, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue