add the flag "directory"
This commit is contained in:
parent
8847df2422
commit
788f2a93d3
49
main.go
49
main.go
|
@ -1,23 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/h2non/filetype"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/urfave/cli/v2"
|
||||
"io/ioutil"
|
||||
|
||||
netUrl "net/url"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var Commands = []*cli.Command{}
|
||||
|
@ -91,10 +92,15 @@ func main() {
|
|||
Aliases: []string{"u"},
|
||||
Value: "http://127.0.0.1:4000",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "directory",
|
||||
Aliases: []string{"d"},
|
||||
Value: "$HOME/Videos",
|
||||
},
|
||||
},
|
||||
Usage: "run client",
|
||||
Action: func(ctx *cli.Context) error {
|
||||
return runShell(ctx, "play")
|
||||
return runShell(ctx, "play", strings.TrimSuffix(ctx.String("directory"), "/"))
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -106,10 +112,15 @@ func main() {
|
|||
Aliases: []string{"u"},
|
||||
Value: "http://127.0.0.1:4000",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "directory",
|
||||
Aliases: []string{"d"},
|
||||
Value: ".",
|
||||
},
|
||||
},
|
||||
Usage: "run client",
|
||||
Action: func(ctx *cli.Context) error {
|
||||
return runShell(ctx, "download")
|
||||
return runShell(ctx, "download", strings.TrimSuffix(ctx.String("directory"), "/"))
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -151,7 +162,6 @@ type ApiError struct {
|
|||
func getFiles(directory, url string) ([]File, error) {
|
||||
files := []File{}
|
||||
absoluteRootPath, err := filepath.Abs(directory)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -207,7 +217,7 @@ func apiStream(e echo.Context, directory, url string) error {
|
|||
if file.RelativePath == path {
|
||||
// stat, _ := os.Stat(file.Path)
|
||||
e.Response().Header().Del("Content-Length")
|
||||
//e.Response().Header().Set("Content-Length", string(stat.Size()))
|
||||
// e.Response().Header().Set("Content-Length", string(stat.Size()))
|
||||
http.ServeFile(e.Response(), e.Request(), file.Path)
|
||||
}
|
||||
}
|
||||
|
@ -217,7 +227,6 @@ func apiStream(e echo.Context, directory, url string) error {
|
|||
|
||||
func apiList(e echo.Context, directory, url string) error {
|
||||
files, err := getFiles(directory, url)
|
||||
|
||||
if err != nil {
|
||||
return e.JSON(http.StatusInternalServerError, ApiError{Error: fmt.Sprintf("%s", err)})
|
||||
}
|
||||
|
@ -225,10 +234,9 @@ func apiList(e echo.Context, directory, url string) error {
|
|||
return e.JSONPretty(http.StatusOK, files, " ")
|
||||
}
|
||||
|
||||
func runShell(ctx *cli.Context, action string) error {
|
||||
func runShell(ctx *cli.Context, action, directory string) error {
|
||||
url := strings.TrimSuffix(ctx.String("api-url"), "/")
|
||||
response, err := http.Get(fmt.Sprintf("%s/api/list", url))
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
|
@ -347,7 +355,7 @@ func runShell(ctx *cli.Context, action string) error {
|
|||
cmds = append(cmds, cmd)
|
||||
} else {
|
||||
for _, f := range result {
|
||||
output := fmt.Sprintf("/tmp/mu/%s", f.Name)
|
||||
output := fmt.Sprintf("%s/%s", directory, f.Name)
|
||||
cmd := exec.Command("wget", "-o", "/dev/stdout", "-c", "--show-progress", "-O", output, f.Url)
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
|
@ -357,7 +365,10 @@ func runShell(ctx *cli.Context, action string) error {
|
|||
stdout, _ := cmd.StdoutPipe()
|
||||
scanner := bufio.NewScanner(stdout)
|
||||
|
||||
cmd.Start()
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
fmt.Printf("%+v\n", err)
|
||||
}
|
||||
|
||||
for scanner.Scan() {
|
||||
out := fmt.Sprintf("%q", scanner.Text())
|
||||
|
|
Loading…
Reference in New Issue