You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
2.9 KiB
Go

package common
import (
"bytes"
"fmt"
"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 int32 `json:"nextType"`
LocalFileName string `json:"-"`
UploadID string `json:"-"`
}
type DB_Chunk struct {
ID string `json:"_key"`
Finalized bool `json:"finalized"`
ReadOnly bool `json:"readOnly"`
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)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", fi.Name())
if err != nil {
return err
}
part.Write(fileContents)
/*for key, val := range params {
_ = writer.WriteField(key, val)
}*/
err = writer.Close()
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, body)
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
}