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.

226 lines
5.8 KiB

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