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.

206 lines
5.3 KiB

9 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
  1. package operation
  2. import (
  3. "bytes"
  4. "io"
  5. "mime"
  6. "net/url"
  7. "os"
  8. "path"
  9. "strconv"
  10. "strings"
  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. IsGzipped bool
  19. MimeType string
  20. ModTime int64 //in seconds
  21. Replication string
  22. Collection 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, files []FilePart,
  35. replication string, collection string, ttl string, maxMB int,
  36. secret security.Secret,
  37. ) ([]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. Ttl: ttl,
  47. }
  48. ret, err := Assign(master, 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. results[index].Size, err = file.Upload(maxMB, master, secret)
  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. if fi, fiErr := fh.Stat(); fiErr != nil {
  89. glog.V(0).Info("Failed to stat file:", fullPathFilename)
  90. return ret, fiErr
  91. } else {
  92. ret.ModTime = fi.ModTime().UTC().Unix()
  93. ret.FileSize = fi.Size()
  94. }
  95. ext := strings.ToLower(path.Ext(fullPathFilename))
  96. ret.IsGzipped = ext == ".gz"
  97. if ret.IsGzipped {
  98. ret.FileName = fullPathFilename[0 : len(fullPathFilename)-3]
  99. }
  100. ret.FileName = fullPathFilename
  101. if ext != "" {
  102. ret.MimeType = mime.TypeByExtension(ext)
  103. }
  104. return ret, nil
  105. }
  106. func (fi FilePart) Upload(maxMB int, master string, secret security.Secret) (retSize uint32, err error) {
  107. jwt := security.GenJwt(secret, fi.Fid)
  108. fileUrl := "http://" + fi.Server + "/" + fi.Fid
  109. if fi.ModTime != 0 {
  110. fileUrl += "?ts=" + strconv.Itoa(int(fi.ModTime))
  111. }
  112. if closer, ok := fi.Reader.(io.Closer); ok {
  113. defer closer.Close()
  114. }
  115. baseName := path.Base(fi.FileName)
  116. if maxMB > 0 && fi.FileSize > int64(maxMB*1024*1024) {
  117. chunkSize := int64(maxMB * 1024 * 1024)
  118. chunks := fi.FileSize/chunkSize + 1
  119. cm := ChunkManifest{
  120. Name: baseName,
  121. Size: fi.FileSize,
  122. Mime: fi.MimeType,
  123. Chunks: make([]*ChunkInfo, 0, chunks),
  124. }
  125. for i := int64(0); i < chunks; i++ {
  126. id, count, e := upload_one_chunk(
  127. baseName+"-"+strconv.FormatInt(i+1, 10),
  128. io.LimitReader(fi.Reader, chunkSize),
  129. master, fi.Replication, fi.Collection, fi.Ttl,
  130. jwt)
  131. if e != nil {
  132. // delete all uploaded chunks
  133. cm.DeleteChunks(master)
  134. return 0, e
  135. }
  136. cm.Chunks = append(cm.Chunks,
  137. &ChunkInfo{
  138. Offset: i * chunkSize,
  139. Size: int64(count),
  140. Fid: id,
  141. },
  142. )
  143. retSize += count
  144. }
  145. err = upload_chunked_file_manifest(fileUrl, &cm, jwt)
  146. if err != nil {
  147. // delete all uploaded chunks
  148. cm.DeleteChunks(master)
  149. }
  150. } else {
  151. ret, e := Upload(fileUrl, baseName, fi.Reader, fi.IsGzipped, fi.MimeType, jwt)
  152. if e != nil {
  153. return 0, e
  154. }
  155. return ret.Size, e
  156. }
  157. return
  158. }
  159. func upload_one_chunk(filename string, reader io.Reader, master,
  160. replication string, collection string, ttl string, jwt security.EncodedJwt,
  161. ) (fid string, size uint32, e error) {
  162. ar := &VolumeAssignRequest{
  163. Count: 1,
  164. Replication: replication,
  165. Collection: collection,
  166. Ttl: ttl,
  167. }
  168. ret, err := Assign(master, ar)
  169. if err != nil {
  170. return "", 0, err
  171. }
  172. fileUrl, fid := "http://"+ret.Url+"/"+ret.Fid, ret.Fid
  173. glog.V(4).Info("Uploading part ", filename, " to ", fileUrl, "...")
  174. uploadResult, uploadError := Upload(fileUrl, filename, reader, false,
  175. "application/octet-stream", jwt)
  176. if uploadError != nil {
  177. return fid, 0, uploadError
  178. }
  179. return fid, uploadResult.Size, nil
  180. }
  181. func upload_chunked_file_manifest(fileUrl string, manifest *ChunkManifest, jwt security.EncodedJwt) error {
  182. buf, e := manifest.Marshal()
  183. if e != nil {
  184. return e
  185. }
  186. bufReader := bytes.NewReader(buf)
  187. glog.V(4).Info("Uploading chunks manifest ", manifest.Name, " to ", fileUrl, "...")
  188. u, _ := url.Parse(fileUrl)
  189. q := u.Query()
  190. q.Set("cm", "true")
  191. u.RawQuery = q.Encode()
  192. _, e = Upload(u.String(), manifest.Name, bufReader, false, "application/json", jwt)
  193. return e
  194. }