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

239 lines
6.2 KiB

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