forked from TrueCloudLab/rclone
webui: Expose webui downloader and other utility for use with plugins
This commit is contained in:
parent
4cf82118d9
commit
65fa6a946a
1 changed files with 25 additions and 9 deletions
|
@ -19,8 +19,8 @@ import (
|
||||||
"github.com/rclone/rclone/lib/errors"
|
"github.com/rclone/rclone/lib/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getLatestReleaseURL returns the latest release details of the rclone-webui-react
|
// GetLatestReleaseURL returns the latest release details of the rclone-webui-react
|
||||||
func getLatestReleaseURL(fetchURL string) (string, string, int, error) {
|
func GetLatestReleaseURL(fetchURL string) (string, string, int, error) {
|
||||||
resp, err := http.Get(fetchURL)
|
resp, err := http.Get(fetchURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", 0, errors.New("Error getting latest release of rclone-webui")
|
return "", "", 0, errors.New("Error getting latest release of rclone-webui")
|
||||||
|
@ -56,7 +56,7 @@ func CheckAndDownloadWebGUIRelease(checkUpdate bool, forceUpdate bool, fetchURL
|
||||||
// TODO: Add hashing to check integrity of the previous update.
|
// TODO: Add hashing to check integrity of the previous update.
|
||||||
if !extractPathExist || checkUpdate || forceUpdate {
|
if !extractPathExist || checkUpdate || forceUpdate {
|
||||||
// Get the latest release details
|
// Get the latest release details
|
||||||
WebUIURL, tag, size, err := getLatestReleaseURL(fetchURL)
|
WebUIURL, tag, size, err := GetLatestReleaseURL(fetchURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ func CheckAndDownloadWebGUIRelease(checkUpdate bool, forceUpdate bool, fetchURL
|
||||||
fs.Logf(nil, "Downloading webgui binary. Please wait. [Size: %s, Path : %s]\n", strconv.Itoa(size), zipPath)
|
fs.Logf(nil, "Downloading webgui binary. Please wait. [Size: %s, Path : %s]\n", strconv.Itoa(size), zipPath)
|
||||||
|
|
||||||
// download the zip from latest url
|
// download the zip from latest url
|
||||||
err = downloadFile(zipPath, WebUIURL)
|
err = DownloadFile(zipPath, WebUIURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -99,7 +99,7 @@ func CheckAndDownloadWebGUIRelease(checkUpdate bool, forceUpdate bool, fetchURL
|
||||||
}
|
}
|
||||||
fs.Logf(nil, "Unzipping webgui binary")
|
fs.Logf(nil, "Unzipping webgui binary")
|
||||||
|
|
||||||
err = unzip(zipPath, extractPath)
|
err = Unzip(zipPath, extractPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -120,8 +120,8 @@ func CheckAndDownloadWebGUIRelease(checkUpdate bool, forceUpdate bool, fetchURL
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// downloadFile is a helper function to download a file from url to the filepath
|
// DownloadFile is a helper function to download a file from url to the filepath
|
||||||
func downloadFile(filepath string, url string) error {
|
func DownloadFile(filepath string, url string) error {
|
||||||
|
|
||||||
// Get the data
|
// Get the data
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
|
@ -142,8 +142,8 @@ func downloadFile(filepath string, url string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// unzip is a helper function to unzip a file specified in src to path dest
|
// Unzip is a helper function to Unzip a file specified in src to path dest
|
||||||
func unzip(src, dest string) (err error) {
|
func Unzip(src, dest string) (err error) {
|
||||||
dest = filepath.Clean(dest) + string(os.PathSeparator)
|
dest = filepath.Clean(dest) + string(os.PathSeparator)
|
||||||
|
|
||||||
r, err := zip.OpenReader(src)
|
r, err := zip.OpenReader(src)
|
||||||
|
@ -213,6 +213,22 @@ func exists(path string) (existence bool, stat os.FileInfo, err error) {
|
||||||
return false, stat, err
|
return false, stat, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreatePathIfNotExist creates the path to a folder if it does not exist
|
||||||
|
func CreatePathIfNotExist(path string) (err error) {
|
||||||
|
exists, stat, _ := exists(path)
|
||||||
|
if !exists {
|
||||||
|
if err := os.MkdirAll(path, 0755); err != nil {
|
||||||
|
return errors.New("Error creating : " + path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if exists && !stat.IsDir() {
|
||||||
|
return errors.New("Path is a file instead of folder. Please check it " + path)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// gitHubRequest Maps the GitHub API request to structure
|
// gitHubRequest Maps the GitHub API request to structure
|
||||||
type gitHubRequest struct {
|
type gitHubRequest struct {
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
|
|
Loading…
Add table
Reference in a new issue