package common import ( "fmt" "io" "io/ioutil" "mime/multipart" "net/http" "os" "time" "git.cheetah.cat/worksucc/gma-puzzles/gma" ) type DB_GMA struct { ID string `json:"_key"` BatchID string `json:"batch"` ProcessingStart time.Time `json:"processingStart"` ProcessingEnd time.Time `json:"processingEnd"` ProcessingDuration int64 `json:"processingDuration"` OriginalPath string `json:"originalPath"` StatModTime time.Time `json:"statModTime"` GMASize int64 `json:"archiveSize"` OptimizedSize int64 `json:"gmasmSize"` GMAHash string `json:"archiveHash"` FooterAddonCRC uint32 `json:"footerCRC"` Header gma.GMAHeader `json:"header"` FirstType int32 `json:"firstType"` Success bool `json:"success"` } type DB_File struct { ID string `json:"_key"` BatchID string `json:"batch"` InitialPath string `json:"initialPath"` Extension string `json:"extension"` Size int64 `json:"size"` CRC uint32 `json:"crc"` Hash string `json:"hash"` } type DB_GMA2File struct { ID string `json:"_key"` BatchID string `json:"batch"` File string `json:"_to"` GMA string `json:"_from"` FileNumber int32 `json:"fileNumber"` FileName string `json:"fileName"` Offset int64 `json:"offset"` FileSize int64 `json:"size"` CRC uint32 `json:"crc"` CRCMatch bool `json:"crcMatch"` NextType uint32 `json:"nextType"` LocalFileName string `json:"-"` UploadID string `json:"-"` } type DB_Chunk struct { ID string `json:"_key"` NotReady bool `json:"notReady"` Finalized bool `json:"finalized"` ReadOnly bool `json:"readOnly"` FileCount int `json:"fileCount"` Size int64 `json:"size"` Hash string `json:"hash"` } type DB_File2Chunk struct { ID string `json:"_key"` Chunk string `json:"_to"` File string `json:"_from"` } func MultipartUpload(client *http.Client, url string, path string) (err error) { //fmt.Printf("\nMultipartUpload(%s, %s)\n", url, path) file, err := os.Open(path) if err != nil { return err } fileContents, err := ioutil.ReadAll(file) if err != nil { return err } fi, err := file.Stat() if err != nil { return err } file.Close() //body := new(bytes.Buffer) pr, pw := io.Pipe() writer := multipart.NewWriter(pw) go func() { defer pw.Close() part, err := writer.CreateFormFile("file", fi.Name()) if err != nil { return } part.Write(fileContents) //defer body.Reset() /*for key, val := range params { _ = writer.WriteField(key, val) }*/ err = writer.Close() if err != nil { return } }() req, err := http.NewRequest("POST", url, pr) req.Header.Set("Content-Type", writer.FormDataContentType()) if err != nil { return err } // Submit the request res, err := client.Do(req) if err != nil { return err } // Check the response if res.StatusCode == http.StatusAlreadyReported { return } if res.StatusCode != http.StatusOK { return fmt.Errorf("bad status: %s", res.Status) } return }