Initial Commit
Internal Link Generation
This commit is contained in:
62
util/cli.go
Normal file
62
util/cli.go
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
||||
*/
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func PromptUser(text string) string {
|
||||
var resp string
|
||||
fmt.Println(text)
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
if scanner.Scan() {
|
||||
resp = scanner.Text()
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func PromptUserOptTrimmed(text string, def string) string {
|
||||
var resp string
|
||||
if def == "" {
|
||||
resp = strings.TrimSpace(PromptUser(text))
|
||||
} else {
|
||||
resp = strings.TrimSpace(PromptUser(fmt.Sprintf("%s [%s]:", text, def)))
|
||||
if resp == "" {
|
||||
resp = def
|
||||
}
|
||||
}
|
||||
for resp == "" {
|
||||
resp = strings.TrimSpace(PromptUser(text))
|
||||
if resp == "" {
|
||||
fmt.Println("Cannot be blank")
|
||||
}
|
||||
}
|
||||
return resp
|
||||
}
|
||||
func PromptUserTrimmed(text string) string {
|
||||
return PromptUserOptTrimmed(text, "")
|
||||
}
|
||||
|
||||
func PromptUserInt(text string) int {
|
||||
var resp string
|
||||
var i int
|
||||
for resp == "" {
|
||||
resp = strings.TrimSpace(PromptUser(text))
|
||||
if resp == "" {
|
||||
fmt.Println("Cannot be blank")
|
||||
}
|
||||
var err error
|
||||
i, err = strconv.Atoi(resp)
|
||||
if err != nil {
|
||||
fmt.Println("Must be a number")
|
||||
resp = ""
|
||||
}
|
||||
}
|
||||
return i
|
||||
}
|
62
util/crypto.go
Normal file
62
util/crypto.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
func getKey() string {
|
||||
key := []byte("4bUMVpSJjvjwEC7qV2AQjvjwEC7qV2AQ")
|
||||
return hex.EncodeToString(key)
|
||||
}
|
||||
func Encrypt(input string) (string, error) {
|
||||
btKey, _ := hex.DecodeString(getKey())
|
||||
plaintext := []byte(input)
|
||||
|
||||
// Create a new Cipher Block from the key
|
||||
block, err := aes.NewCipher(btKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
||||
iv := ciphertext[:aes.BlockSize]
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
stream := cipher.NewCFBEncrypter(block, iv)
|
||||
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
|
||||
|
||||
// Convert to base64
|
||||
return base64.URLEncoding.EncodeToString(ciphertext), nil
|
||||
}
|
||||
|
||||
func Decrypt(input string) (string, error) {
|
||||
btKey, _ := hex.DecodeString(getKey())
|
||||
ciphertext, _ := base64.URLEncoding.DecodeString(input)
|
||||
|
||||
block, err := aes.NewCipher(btKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(ciphertext) < aes.BlockSize {
|
||||
return "", errors.New("ciphertext is too short")
|
||||
}
|
||||
iv := ciphertext[:aes.BlockSize]
|
||||
ciphertext = ciphertext[aes.BlockSize:]
|
||||
|
||||
stream := cipher.NewCFBDecrypter(block, iv)
|
||||
|
||||
// XORKeyStream can work in-place if the two arguments are the same.
|
||||
stream.XORKeyStream(ciphertext, ciphertext)
|
||||
|
||||
return fmt.Sprintf("%s", ciphertext), nil
|
||||
}
|
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