submit.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package operation
  2. import (
  3. "io"
  4. "mime"
  5. "net/url"
  6. "os"
  7. "path"
  8. "strconv"
  9. "strings"
  10. "google.golang.org/grpc"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/security"
  13. )
  14. type FilePart struct {
  15. Reader io.Reader
  16. FileName string
  17. FileSize int64
  18. MimeType string
  19. ModTime int64 //in seconds
  20. Replication string
  21. Collection string
  22. DataCenter string
  23. Ttl string
  24. Server string //this comes from assign result
  25. Fid string //this comes from assign result, but customizable
  26. }
  27. type SubmitResult struct {
  28. FileName string `json:"fileName,omitempty"`
  29. FileUrl string `json:"fileUrl,omitempty"`
  30. Fid string `json:"fid,omitempty"`
  31. Size uint32 `json:"size,omitempty"`
  32. Error string `json:"error,omitempty"`
  33. }
  34. func SubmitFiles(master string, grpcDialOption grpc.DialOption, files []FilePart,
  35. replication string, collection string, dataCenter string, ttl string, maxMB int) ([]SubmitResult, error) {
  36. results := make([]SubmitResult, len(files))
  37. for index, file := range files {
  38. results[index].FileName = file.FileName
  39. }
  40. ar := &VolumeAssignRequest{
  41. Count: uint64(len(files)),
  42. Replication: replication,
  43. Collection: collection,
  44. DataCenter: dataCenter,
  45. Ttl: ttl,
  46. }
  47. ret, err := Assign(master, grpcDialOption, ar)
  48. if err != nil {
  49. for index := range files {
  50. results[index].Error = err.Error()
  51. }
  52. return results, err
  53. }
  54. for index, file := range files {
  55. file.Fid = ret.Fid
  56. if index > 0 {
  57. file.Fid = file.Fid + "_" + strconv.Itoa(index)
  58. }
  59. file.Server = ret.Url
  60. file.Replication = replication
  61. file.Collection = collection
  62. file.DataCenter = dataCenter
  63. results[index].Size, err = file.Upload(maxMB, master, ret.Auth, grpcDialOption)
  64. if err != nil {
  65. results[index].Error = err.Error()
  66. }
  67. results[index].Fid = file.Fid
  68. results[index].FileUrl = ret.PublicUrl + "/" + file.Fid
  69. }
  70. return results, nil
  71. }
  72. func NewFileParts(fullPathFilenames []string) (ret []FilePart, err error) {
  73. ret = make([]FilePart, len(fullPathFilenames))
  74. for index, file := range fullPathFilenames {
  75. if ret[index], err = newFilePart(file); err != nil {
  76. return
  77. }
  78. }
  79. return
  80. }
  81. func newFilePart(fullPathFilename string) (ret FilePart, err error) {
  82. fh, openErr := os.Open(fullPathFilename)
  83. if openErr != nil {
  84. glog.V(0).Info("Failed to open file: ", fullPathFilename)
  85. return ret, openErr
  86. }
  87. ret.Reader = fh
  88. fi, fiErr := fh.Stat()
  89. if fiErr != nil {
  90. glog.V(0).Info("Failed to stat file:", fullPathFilename)
  91. return ret, fiErr
  92. }
  93. ret.ModTime = fi.ModTime().UTC().Unix()
  94. ret.FileSize = fi.Size()
  95. ext := strings.ToLower(path.Ext(fullPathFilename))
  96. ret.FileName = fi.Name()
  97. if ext != "" {
  98. ret.MimeType = mime.TypeByExtension(ext)
  99. }
  100. return ret, nil
  101. }
  102. func (fi FilePart) Upload(maxMB int, master string, jwt security.EncodedJwt, grpcDialOption grpc.DialOption) (retSize uint32, err error) {
  103. fileUrl := "http://" + fi.Server + "/" + fi.Fid
  104. if fi.ModTime != 0 {
  105. fileUrl += "?ts=" + strconv.Itoa(int(fi.ModTime))
  106. }
  107. if closer, ok := fi.Reader.(io.Closer); ok {
  108. defer closer.Close()
  109. }
  110. baseName := path.Base(fi.FileName)
  111. if maxMB > 0 && fi.FileSize > int64(maxMB*1024*1024) {
  112. chunkSize := int64(maxMB * 1024 * 1024)
  113. chunks := fi.FileSize/chunkSize + 1
  114. cm := ChunkManifest{
  115. Name: baseName,
  116. Size: fi.FileSize,
  117. Mime: fi.MimeType,
  118. Chunks: make([]*ChunkInfo, 0, chunks),
  119. }
  120. var ret *AssignResult
  121. var id string
  122. if fi.DataCenter != "" {
  123. ar := &VolumeAssignRequest{
  124. Count: uint64(chunks),
  125. Replication: fi.Replication,
  126. Collection: fi.Collection,
  127. Ttl: fi.Ttl,
  128. }
  129. ret, err = Assign(master, grpcDialOption, ar)
  130. if err != nil {
  131. return
  132. }
  133. }
  134. for i := int64(0); i < chunks; i++ {
  135. if fi.DataCenter == "" {
  136. ar := &VolumeAssignRequest{
  137. Count: 1,
  138. Replication: fi.Replication,
  139. Collection: fi.Collection,
  140. Ttl: fi.Ttl,
  141. }
  142. ret, err = Assign(master, grpcDialOption, ar)
  143. if err != nil {
  144. // delete all uploaded chunks
  145. cm.DeleteChunks(master, grpcDialOption)
  146. return
  147. }
  148. id = ret.Fid
  149. } else {
  150. id = ret.Fid
  151. if i > 0 {
  152. id += "_" + strconv.FormatInt(i, 10)
  153. }
  154. }
  155. fileUrl := "http://" + ret.Url + "/" + id
  156. count, e := upload_one_chunk(
  157. baseName+"-"+strconv.FormatInt(i+1, 10),
  158. io.LimitReader(fi.Reader, chunkSize),
  159. master, fileUrl,
  160. ret.Auth)
  161. if e != nil {
  162. // delete all uploaded chunks
  163. cm.DeleteChunks(master, grpcDialOption)
  164. return 0, e
  165. }
  166. cm.Chunks = append(cm.Chunks,
  167. &ChunkInfo{
  168. Offset: i * chunkSize,
  169. Size: int64(count),
  170. Fid: id,
  171. },
  172. )
  173. retSize += count
  174. }
  175. err = upload_chunked_file_manifest(fileUrl, &cm, jwt)
  176. if err != nil {
  177. // delete all uploaded chunks
  178. cm.DeleteChunks(master, grpcDialOption)
  179. }
  180. } else {
  181. ret, e := Upload(fileUrl, baseName, false, fi.Reader, false, fi.MimeType, nil, jwt)
  182. if e != nil {
  183. return 0, e
  184. }
  185. return ret.Size, e
  186. }
  187. return
  188. }
  189. func upload_one_chunk(filename string, reader io.Reader, master,
  190. fileUrl string, jwt security.EncodedJwt,
  191. ) (size uint32, e error) {
  192. glog.V(4).Info("Uploading part ", filename, " to ", fileUrl, "...")
  193. uploadResult, uploadError := Upload(fileUrl, filename, false, reader, false, "", nil, jwt)
  194. if uploadError != nil {
  195. return 0, uploadError
  196. }
  197. return uploadResult.Size, nil
  198. }
  199. func upload_chunked_file_manifest(fileUrl string, manifest *ChunkManifest, jwt security.EncodedJwt) error {
  200. buf, e := manifest.Marshal()
  201. if e != nil {
  202. return e
  203. }
  204. glog.V(4).Info("Uploading chunks manifest ", manifest.Name, " to ", fileUrl, "...")
  205. u, _ := url.Parse(fileUrl)
  206. q := u.Query()
  207. q.Set("cm", "true")
  208. u.RawQuery = q.Encode()
  209. _, e = UploadData(u.String(), manifest.Name, false, buf, false, "application/json", nil, jwt)
  210. return e
  211. }