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.

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