- 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
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"ticket-pimp/domain"
|
||||
d "ticket-pimp/domain"
|
||||
"ticket-pimp/helpers"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -17,8 +13,8 @@ type Cloud struct {
|
|||
}
|
||||
|
||||
type ICloud interface {
|
||||
CreateFolder(name string) (*domain.Cloud, error)
|
||||
ShareToExternals(cloud *domain.Cloud) (*domain.Cloud, error)
|
||||
CreateFolder(name string) (*d.Folder, error)
|
||||
ShareToExternals(cloud *d.Folder) (*d.Folder, error)
|
||||
}
|
||||
|
||||
func NewCloud(base, user, pass string) *Cloud {
|
||||
|
|
@ -35,83 +31,71 @@ func NewCloud(base, user, pass string) *Cloud {
|
|||
}
|
||||
}
|
||||
|
||||
type MultistatusObj struct {
|
||||
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) {
|
||||
func (c *Cloud) CreateFolder(name string) (*d.Folder, error) {
|
||||
rootDir := os.Getenv("ROOTDIR")
|
||||
user := os.Getenv("CLOUD_USER")
|
||||
|
||||
davPath := "/remote.php/dav/files/"
|
||||
parentPath := "/apps/files/?dir="
|
||||
|
||||
name = helpers.GitNaming(name)
|
||||
|
||||
cloud := domain.Cloud{
|
||||
FolderName: name,
|
||||
FolderURL: "",
|
||||
cloud := d.Folder{
|
||||
Title: name,
|
||||
PrivateURL: "",
|
||||
}
|
||||
|
||||
requestPath := os.Getenv("HOMEPATH") + rootDir + name
|
||||
cloud.FolderPath = os.Getenv("FOLDER_PATH") + rootDir + name
|
||||
requestPath := davPath + user + rootDir + name
|
||||
|
||||
cloud.PathTo = parentPath + rootDir + name
|
||||
|
||||
resp, err := c.R().
|
||||
Send("MKCOL", requestPath)
|
||||
|
||||
if resp.IsSuccessState() {
|
||||
// Set stupid URL to the d entity
|
||||
cloud.PrivateURL = c.BaseURL + cloud.PathTo
|
||||
|
||||
cloud.FolderURL = c.BaseURL + cloud.FolderPath
|
||||
|
||||
xmlFile, err := ioutil.ReadFile("./fileid.xml")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err // fix this return;
|
||||
// Try to set short URL to the d entity
|
||||
if err = c.setPrivateURL(requestPath, &cloud); err != nil {
|
||||
return &cloud, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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..
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("request xml file error: %v", err)
|
||||
}
|
||||
*/
|
||||
|
||||
resp, err := c.R().
|
||||
SetBody(payload).
|
||||
Send("PROPFIND", requestPath)
|
||||
|
||||
err := xml.Unmarshal(str, &multi)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("XML Unmarshal error: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(multi.Multistatus.Propstat.Prop.FileID.ID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("FileID str to int convertion error: %v", err)
|
||||
id := helpers.GetFileIDFromRespBody(resp.Bytes())
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue