2018-10-27 17:02:45 +02:00
|
|
|
package rcd
|
|
|
|
|
|
|
|
import (
|
2019-08-04 13:32:37 +02:00
|
|
|
"archive/zip"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2018-10-27 17:02:45 +02:00
|
|
|
"log"
|
2019-08-04 13:32:37 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2018-10-27 17:02:45 +02:00
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2019-08-04 13:32:37 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/rc/rcflags"
|
|
|
|
"github.com/rclone/rclone/fs/rc/rcserver"
|
2019-08-04 13:32:37 +02:00
|
|
|
"github.com/rclone/rclone/lib/errors"
|
2018-10-27 17:02:45 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-08-04 13:32:37 +02:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2018-10-27 17:02:45 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 13:32:37 +02:00
|
|
|
var commandDefinition = &cobra.Command{
|
2018-10-27 17:02:45 +02:00
|
|
|
Use: "rcd <path to files to serve>*",
|
|
|
|
Short: `Run rclone listening to remote control commands only.`,
|
|
|
|
Long: `
|
2019-01-29 15:12:26 +01:00
|
|
|
This runs rclone so that it only listens to remote control commands.
|
2018-10-27 17:02:45 +02:00
|
|
|
|
|
|
|
This is useful if you are controlling rclone via the rc API.
|
|
|
|
|
|
|
|
If you pass in a path to a directory, rclone will serve that directory
|
|
|
|
for GET requests on the URL passed in. It will also open the URL in
|
|
|
|
the browser when rclone is run.
|
2018-10-28 15:31:24 +01:00
|
|
|
|
|
|
|
See the [rc documentation](/rc/) for more info on the rc flags.
|
2018-10-27 17:02:45 +02:00
|
|
|
`,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(0, 1, command, args)
|
|
|
|
if rcflags.Opt.Enabled {
|
|
|
|
log.Fatalf("Don't supply --rc flag when using rcd")
|
|
|
|
}
|
2019-08-04 13:32:37 +02:00
|
|
|
|
2018-10-27 17:02:45 +02:00
|
|
|
// Start the rc
|
|
|
|
rcflags.Opt.Enabled = true
|
|
|
|
if len(args) > 0 {
|
|
|
|
rcflags.Opt.Files = args[0]
|
|
|
|
}
|
2019-08-04 13:32:37 +02:00
|
|
|
|
|
|
|
if rcflags.Opt.WebUI {
|
|
|
|
if err := checkRelease(rcflags.Opt.WebGUIUpdate); err != nil {
|
|
|
|
log.Fatalf("Error while fetching the latest release of rclone-webui-react %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 18:20:04 +01:00
|
|
|
s, err := rcserver.Start(&rcflags.Opt)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to start remote control: %v", err)
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
log.Fatal("rc server not configured")
|
|
|
|
}
|
2019-08-04 13:32:37 +02:00
|
|
|
|
2018-11-01 18:20:04 +01:00
|
|
|
s.Wait()
|
2018-10-27 17:02:45 +02:00
|
|
|
},
|
|
|
|
}
|
2019-08-04 13:32:37 +02:00
|
|
|
|
|
|
|
//checkRelease is a helper function to download and setup latest release of rclone-webui-react
|
|
|
|
func checkRelease(shouldUpdate bool) (err error) {
|
|
|
|
// Get the latest release details
|
|
|
|
WebUIURL, tag, size, err := getLatestReleaseURL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
zipName := tag + ".zip"
|
|
|
|
cachePath := filepath.Join(config.CacheDir, "webgui")
|
|
|
|
zipPath := filepath.Join(cachePath, zipName)
|
|
|
|
extractPath := filepath.Join(cachePath, "current")
|
|
|
|
|
|
|
|
if !exists(cachePath) {
|
2019-08-06 13:06:40 +02:00
|
|
|
if err := os.MkdirAll(cachePath, 0755); err != nil {
|
2019-08-04 13:32:37 +02:00
|
|
|
fs.Logf(nil, "Error creating cache directory: %s", cachePath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Load the file
|
|
|
|
exists := exists(zipPath)
|
|
|
|
// if the zipFile does not exist or forced update is enforced.
|
|
|
|
if !exists || shouldUpdate {
|
|
|
|
fs.Logf(nil, "A new release for gui is present at "+WebUIURL)
|
|
|
|
fs.Logf(nil, "Downloading webgui binary. Please wait. [Size: %s, Path : %s]\n", strconv.Itoa(size), zipPath)
|
|
|
|
err := downloadFile(zipPath, WebUIURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(extractPath)
|
|
|
|
if err != nil {
|
|
|
|
fs.Logf(nil, "No previous downloads to remove")
|
|
|
|
}
|
|
|
|
fs.Logf(nil, "Unzipping")
|
|
|
|
err = unzip(zipPath, extractPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
fs.Logf(nil, "Required files exist. Skipping download")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getLatestReleaseURL returns the latest release details of the rclone-webui-react
|
|
|
|
func getLatestReleaseURL() (string, string, int, error) {
|
|
|
|
resp, err := http.Get(rcflags.Opt.WebGUIFetchURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", 0, errors.New("Error getting latest release of rclone-webui")
|
|
|
|
}
|
|
|
|
results := gitHubRequest{}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&results); err != nil {
|
|
|
|
return "", "", 0, errors.New("Could not decode results from http request")
|
|
|
|
}
|
|
|
|
|
|
|
|
res := results.Assets[0].BrowserDownloadURL
|
|
|
|
tag := results.TagName
|
|
|
|
size := results.Assets[0].Size
|
|
|
|
//fmt.Println( "URL:" + res)
|
|
|
|
|
|
|
|
return res, tag, size, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// downloadFile is a helper function to download a file from url to the filepath
|
|
|
|
func downloadFile(filepath string, url string) error {
|
|
|
|
|
|
|
|
// Get the data
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(resp.Body, &err)
|
|
|
|
|
|
|
|
// Create the file
|
|
|
|
out, err := os.Create(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(out, &err)
|
|
|
|
|
|
|
|
// Write the body to file
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// unzip is a helper function to unzip a file specified in src to path dest
|
|
|
|
func unzip(src, dest string) (err error) {
|
|
|
|
r, err := zip.OpenReader(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(r, &err)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(dest, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Closure to address file descriptors issue with all the deferred .Close() methods
|
|
|
|
extractAndWriteFile := func(f *zip.File) error {
|
|
|
|
rc, err := f.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(rc, &err)
|
|
|
|
|
|
|
|
path := filepath.Join(dest, f.Name)
|
|
|
|
|
|
|
|
if f.FileInfo().IsDir() {
|
2019-08-06 13:06:40 +02:00
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
2019-08-04 13:32:37 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-06 13:06:40 +02:00
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
2019-08-04 13:32:37 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-08-06 13:06:40 +02:00
|
|
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
2019-08-04 13:32:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(f, &err)
|
|
|
|
|
|
|
|
_, err = io.Copy(f, rc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range r.File {
|
|
|
|
err := extractAndWriteFile(f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// exists returns whether the given file or directory exists
|
|
|
|
func exists(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// gitHubRequest Maps the GitHub API request to structure
|
|
|
|
type gitHubRequest struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
|
|
|
|
Prerelease bool `json:"prerelease"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
PublishedAt time.Time `json:"published_at"`
|
|
|
|
TagName string `json:"tag_name"`
|
|
|
|
Assets []struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
NodeID string `json:"node_id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
ContentType string `json:"content_type"`
|
|
|
|
State string `json:"state"`
|
|
|
|
Size int `json:"size"`
|
|
|
|
DownloadCount int `json:"download_count"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
|
|
BrowserDownloadURL string `json:"browser_download_url"`
|
|
|
|
} `json:"assets"`
|
|
|
|
TarballURL string `json:"tarball_url"`
|
|
|
|
ZipballURL string `json:"zipball_url"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
}
|