|
|
@ -1,10 +1,14 @@
|
|
|
|
package pic
|
|
|
|
package pic
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
//"log"
|
|
|
|
//"log"
|
|
|
|
"github.com/linxGnu/goseaweedfs"
|
|
|
|
"github.com/linxGnu/goseaweedfs"
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -46,10 +50,44 @@ func delete(url string) error {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// загружает файл из weed в filename
|
|
|
|
|
|
|
|
func download(url string, filename string) error {
|
|
|
|
|
|
|
|
fid, err := getFID(url)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_, err = sw.Download(fid, nil, func(r io.Reader) (err error) {
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(r)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.OpenFile(filename, os.O_CREATE, 0666)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = file.Write(data)
|
|
|
|
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getFID(url string) (string, error) {
|
|
|
|
func getFID(url string) (string, error) {
|
|
|
|
items := strings.Split(url, "/")
|
|
|
|
items := strings.Split(url, "/")
|
|
|
|
if len(items) != 2 {
|
|
|
|
if len(items) < 2 {
|
|
|
|
return "", fmt.Errorf("url должен быть в формате: <address/fid>")
|
|
|
|
return "", fmt.Errorf("url должен быть в формате: <http://address/fid.ext>")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return items[len(items)-1], nil
|
|
|
|
return items[len(items)-1], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getFilename(url string) (string, error) {
|
|
|
|
|
|
|
|
fn, err := getFID(url)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
items := strings.Split(fn, ",")
|
|
|
|
|
|
|
|
return items[len(items)-1], nil
|
|
|
|
|
|
|
|
}
|
|
|
|