Skip to content

Commit

Permalink
支持通过 URL 上传文件. closed #1
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Jun 26, 2016
1 parent 0034cff commit 3b2f21c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ http://tmp-images.qiniudn.com/test/124.txt
$ qn_cli -d test *.txt
http://tmp-images.qiniudn.com/test/1234.txt
http://tmp-images.qiniudn.com/test/2345.txt
$ qn_cli -d test http://aiohttp.readthedocs.io/en/stable/_static/aiohttp-icon-128x128.png LICENSE
https://dn-tmp.qbox.me/test/LICENSE
https://dn-tmp.qbox.me/test/aiohttp-icon-128x128.png
$ qn_cli --help
Expand Down
54 changes: 53 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import (
"encoding/hex"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
url2 "net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"time"

Expand All @@ -30,6 +34,7 @@ func Version() (v string) {
var ignorePaths = []string{
".git", ".hg", ".svn", ".module-cache", ".bin",
}
var tempDir = ""

type stringSlice []string

Expand Down Expand Up @@ -74,6 +79,9 @@ func uploadFile(
func autoFileName(p string) (string, string, string) {
dirname, name := path.Split(p)
ext := path.Ext(name)
if tempDir != "" && dirname == tempDir {
dirname = ""
}
return dirname, name, ext
}

Expand All @@ -89,7 +97,16 @@ func autoMD5FileName(p string) string {

func walkFiles(files []string, ignorePaths []string) (fileSlice []string) {
for _, file := range files {
if strings.HasPrefix(file, "http://") || strings.HasPrefix(file, "https://") {
if pathx, err := downloadFile(file); err == nil {
file = pathx
} else {
continue
}
}

matches, err := filepath.Glob(file)
_ = "breakpoint"
if err == nil {

for _, path := range matches {
Expand Down Expand Up @@ -133,6 +150,37 @@ func finalURL(bucketURL, key string) (url string) {
return
}

func downloadFile(url string) (pathx string, err error) {
defer func() {
if err != nil {
log.Fatal(err)
}
}()

c := &http.Client{}
resp, err := c.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
dir, err := ioutil.TempDir("", "qn_cli")
tempDir = dir
if err != nil {
return
}

u2, _ := url2.Parse(url)
name := filepath.Base(u2.Path)
pathx = filepath.Join(dir, name)
file, err := os.Create(pathx)
if err != nil {
return
}
defer file.Close()
io.Copy(file, resp.Body)
return
}

type args struct {
bucketName string
bucketURL string
Expand Down Expand Up @@ -243,7 +291,11 @@ func main() {
if a.autoMD5Name && key == "" {
key = autoMD5FileName(file)
} else if a.autoName && key == "" {
key = file
if tempDir != "" && strings.HasPrefix(file, tempDir) {
key = strings.Split(file, tempDir)[1]
} else {
key = file
}
}
if a.saveDir != "" {
key = path.Join(a.saveDir, key)
Expand Down

0 comments on commit 3b2f21c

Please sign in to comment.