|
|
|
@ -1,10 +1,14 @@
|
|
|
|
|
package pic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
//"log"
|
|
|
|
|
"github.com/linxGnu/goseaweedfs"
|
|
|
|
|
)
|
|
|
|
@ -46,10 +50,44 @@ func delete(url string) error {
|
|
|
|
|
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) {
|
|
|
|
|
items := strings.Split(url, "/")
|
|
|
|
|
if len(items) != 2 {
|
|
|
|
|
return "", fmt.Errorf("url должен быть в формате: <address/fid>")
|
|
|
|
|
if len(items) < 2 {
|
|
|
|
|
return "", fmt.Errorf("url должен быть в формате: <http://address/fid.ext>")
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|