| | |
| | | "mime/multipart" |
| | | "net/http" |
| | | "github.com/kirinlabs/HttpRequest" |
| | | "strings" |
| | | "time" |
| | | ) |
| | | |
| | | func UploadFile(uri string, fileName string, fileData []byte) (weedFilePath string, err error) { |
| | | 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) |
| | |
| | | //writer.WriteField(key, val) |
| | | request, err := http.NewRequest("POST", uri, request_reader) |
| | | request.Header.Add("Content-Type", writer.FormDataContentType()) |
| | | timeout := time.Duration(5 * time.Second) //超时时间50ms |
| | | if timeout <=0 { |
| | | timeout = 5 * time.Second |
| | | } |
| | | client := &http.Client{Timeout: timeout} |
| | | resp, err := client.Do(request) |
| | | if err != nil { |
| | |
| | | return "", err |
| | | } |
| | | filePath := m["fileUrl"].(string) |
| | | return filePath,nil |
| | | |
| | | return appendCollection2Uri(uri, filePath),nil |
| | | } |
| | | } |
| | | |
| | | // 获得一个fid |
| | | 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 |
| | | } |
| | | return "http://"+m["url"].(string)+"/"+m["fid"].(string),err |
| | | u,ok1 := m["url"] |
| | | 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) (m map[string]interface{}, err error) { |
| | | 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) |
| | |
| | | //writer.WriteField(key, val) |
| | | request, err := http.NewRequest("POST", uri, request_reader) |
| | | request.Header.Add("Content-Type", writer.FormDataContentType()) |
| | | timeout := time.Duration(5 * time.Second) //超时时间50ms |
| | | if timeout <= 0 { |
| | | timeout = time.Duration(5 * time.Second) //超时时间5s |
| | | } |
| | | client := &http.Client{Timeout: timeout} |
| | | resp, err := client.Do(request) |
| | | if err != nil { |