submit.go 6.3 KB

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