New file |
| | |
| | | package WeedFSClient |
| | | |
| | | import ( |
| | | "bytes" |
| | | "encoding/json" |
| | | "errors" |
| | | "fmt" |
| | | "github.com/kirinlabs/HttpRequest" |
| | | "io" |
| | | "mime/multipart" |
| | | "net/http" |
| | | "strings" |
| | | "time" |
| | | ) |
| | | |
| | | func UploadFile(uri string, fileName string, fileData []byte, timeout time.Duration) (weedFilePath string, err error) { |
| | | body := &bytes.Buffer{} |
| | | writer := multipart.NewWriter(body) |
| | | _, err = writer.CreateFormFile("file", fileName) |
| | | if err != nil { |
| | | return "", err |
| | | } |
| | | boundary := writer.Boundary() |
| | | //close_string := fmt.Sprintf("\r\n--%s--\r\n", boundary) |
| | | close_buf := bytes.NewBufferString(fmt.Sprintf("\r\n--%s--\r\n", boundary)) |
| | | file := bytes.NewBuffer(fileData) |
| | | request_reader := io.MultiReader(body, file, close_buf) |
| | | //_, err = io.Copy(part, file) |
| | | //writer.WriteField(key, val) |
| | | request, err := http.NewRequest("POST", uri, request_reader) |
| | | request.Header.Add("Content-Type", writer.FormDataContentType()) |
| | | if timeout <= 0 { |
| | | timeout = 5 * time.Second |
| | | } |
| | | client := &http.Client{Timeout: timeout} |
| | | resp, err := client.Do(request) |
| | | if err != nil { |
| | | fmt.Println("UploadFile client.Do(request) err:", err) |
| | | return "", err |
| | | } |
| | | defer func() { |
| | | if r := recover(); r != nil { |
| | | fmt.Printf("panic的内容%v\n", r) |
| | | msg := "上传图片服务器异常" |
| | | if _, ok := r.(error); ok { |
| | | msg = r.(error).Error() |
| | | fmt.Println("panic--recover()得到的是error类型") |
| | | } |
| | | if _, ok := r.(string); ok { |
| | | msg = r.(string) |
| | | fmt.Println("panic--recover()得到的是string类型") |
| | | } |
| | | err = errors.New(msg) |
| | | } |
| | | }() |
| | | defer resp.Body.Close() |
| | | { |
| | | body := &bytes.Buffer{} |
| | | _, err := body.ReadFrom(resp.Body) |
| | | if err != nil { |
| | | fmt.Println("UploadFile body.ReadForm(resp.Body) err:", err) |
| | | return "", err |
| | | } |
| | | |
| | | m := make(map[string]interface{}) |
| | | if err := json.Unmarshal([]byte(body.String()), &m); err != nil { |
| | | return "", err |
| | | } |
| | | if furl, ok := m["fileUrl"]; ok { |
| | | filePath := furl.(string) |
| | | return appendCollection2Uri(uri, filePath), nil |
| | | } else { |
| | | fmt.Println("no fileUrl in m, m:", m) |
| | | return "", errors.New("file server err") |
| | | } |
| | | } |
| | | } |
| | | |
| | | func appendCollection2Uri(weedfsUri, fileUri string) string { |
| | | //判断weedfsUri中是否包含collection参数 |
| | | idx := strings.Index(weedfsUri, "?") |
| | | if idx > -1 { |
| | | return fileUri + weedfsUri[idx:] |
| | | } else { |
| | | return fileUri |
| | | } |
| | | } |
| | | |
| | | // 获得一个fid url eg:http://192.168.5.23:6333/assign/dir?collection=11111-persistent |
| | | func GetFid(url string) (weedPath string, err error) { |
| | | res, err := HttpRequest.NewRequest().Post(url) |
| | | if err != nil { |
| | | fmt.Println("网络传输错误!") |
| | | return "", err |
| | | } |
| | | var m map[string]interface{} |
| | | err = res.Json(&m) |
| | | if err != nil { |
| | | return "", err |
| | | } |
| | | u, ok1 := m["publicUrl"] |
| | | f, ok2 := m["fid"] |
| | | if ok1 && ok2 { |
| | | filePath := "http://" + u.(string) + "/" + f.(string) |
| | | return appendCollection2Uri(url, filePath), err |
| | | } else { |
| | | return "", errors.New("未获取到url和fid") |
| | | } |
| | | } |
| | | |
| | | func UpDateFile(uri string, fileName string, fileData []byte, timeout time.Duration) (m map[string]interface{}, err error) { |
| | | body := &bytes.Buffer{} |
| | | writer := multipart.NewWriter(body) |
| | | _, err = writer.CreateFormFile("file", fileName) |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | boundary := writer.Boundary() |
| | | //close_string := fmt.Sprintf("\r\n--%s--\r\n", boundary) |
| | | close_buf := bytes.NewBufferString(fmt.Sprintf("\r\n--%s--\r\n", boundary)) |
| | | file := bytes.NewBuffer(fileData) |
| | | request_reader := io.MultiReader(body, file, close_buf) |
| | | //_, err = io.Copy(part, file) |
| | | //writer.WriteField(key, val) |
| | | request, err := http.NewRequest("POST", uri, request_reader) |
| | | request.Header.Add("Content-Type", writer.FormDataContentType()) |
| | | if timeout <= 0 { |
| | | timeout = time.Duration(5 * time.Second) //超时时间5s |
| | | } |
| | | client := &http.Client{Timeout: timeout} |
| | | resp, err := client.Do(request) |
| | | if err != nil { |
| | | fmt.Println("UploadFile client.Do(request) err:", err) |
| | | return nil, err |
| | | } |
| | | defer func() { |
| | | if r := recover(); r != nil { |
| | | fmt.Printf("panic的内容%v\n", r) |
| | | msg := "上传图片服务器异常" |
| | | if _, ok := r.(error); ok { |
| | | msg = r.(error).Error() |
| | | fmt.Println("panic--recover()得到的是error类型") |
| | | } |
| | | if _, ok := r.(string); ok { |
| | | msg = r.(string) |
| | | fmt.Println("panic--recover()得到的是string类型") |
| | | } |
| | | err = errors.New(msg) |
| | | } |
| | | }() |
| | | defer resp.Body.Close() |
| | | { |
| | | body := &bytes.Buffer{} |
| | | _, err := body.ReadFrom(resp.Body) |
| | | if err != nil { |
| | | fmt.Println("UploadFile body.ReadForm(resp.Body) err:", err) |
| | | } |
| | | |
| | | m := make(map[string]interface{}) |
| | | if err := json.Unmarshal([]byte(body.String()), &m); err != nil { |
| | | return nil, err |
| | | } |
| | | return m, nil |
| | | } |
| | | } |