Initial Commit
Internal Link Generation
This commit is contained in:
71
util/nextcloud.go
Normal file
71
util/nextcloud.go
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
|
||||
*/
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func GetAuth() (string, error) {
|
||||
username := viper.GetString("ncuser")
|
||||
password, err := Decrypt(viper.GetString("ncpw"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
auth := username + ":" + password
|
||||
return base64.StdEncoding.EncodeToString([]byte(auth)), nil
|
||||
}
|
||||
|
||||
func NormalizeLocalPath(path string) string {
|
||||
root := viper.GetString("directory")
|
||||
return strings.TrimPrefix(path, root)
|
||||
}
|
||||
|
||||
func GetFilesUrl(path string) string {
|
||||
return fmt.Sprintf("%sremote.php/dav/files/%s/%s", viper.GetString("ncurl"), viper.GetString("ncuser"), path)
|
||||
}
|
||||
|
||||
func GetFileId(url string) (string, error) {
|
||||
var auth string
|
||||
client := &http.Client{}
|
||||
data := bytes.NewBuffer([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<d:propfind xmlns:d="DAV:">
|
||||
<d:prop xmlns:oc="http://owncloud.org/ns">
|
||||
<oc:fileid/>
|
||||
</d:prop>
|
||||
</d:propfind>`))
|
||||
req, err := http.NewRequest("PROPFIND", url, data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
auth, err = GetAuth()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Add("Authorization", "Basic "+auth)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var body []byte
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
wrkArr := strings.Split(string(body), "<oc:fileid>")
|
||||
if len(wrkArr) != 2 {
|
||||
return "", errors.New("Couldn't find File ID")
|
||||
}
|
||||
wrkArr = strings.Split(wrkArr[1], "</oc:fileid>")
|
||||
if len(wrkArr) != 2 {
|
||||
return "", errors.New("Couldn't find File ID")
|
||||
}
|
||||
return wrkArr[0], nil
|
||||
}
|
Reference in New Issue
Block a user