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.

147 lines
3.9 KiB

  1. package operation
  2. import (
  3. "bytes"
  4. "code.google.com/p/weed-fs/go/glog"
  5. "io"
  6. "mime"
  7. "os"
  8. "path"
  9. "strconv"
  10. "strings"
  11. )
  12. type FilePart struct {
  13. Reader io.Reader
  14. FileName string
  15. FileSize int64
  16. IsGzipped bool
  17. MimeType string
  18. ModTime int64 //in seconds
  19. }
  20. type SubmitResult struct {
  21. FileName string `json:"fileName"`
  22. FileUrl string `json:"fileUrl"`
  23. Fid string `json:"fid"`
  24. Size int `json:"size"`
  25. Error string `json:"error"`
  26. }
  27. func SubmitFiles(master string, files []FilePart, replication string, maxMB int) ([]SubmitResult, error) {
  28. results := make([]SubmitResult, len(files))
  29. for index, file := range files {
  30. results[index].FileName = file.FileName
  31. }
  32. ret, err := Assign(master, len(files), replication)
  33. if err != nil {
  34. for index, _ := range files {
  35. results[index].Error = err.Error()
  36. }
  37. return results, err
  38. }
  39. for index, file := range files {
  40. fid := ret.Fid
  41. if index > 0 {
  42. fid = fid + "_" + strconv.Itoa(index)
  43. }
  44. results[index].Size, err = file.upload(ret.PublicUrl, fid, maxMB, master, replication)
  45. if err != nil {
  46. fid = ""
  47. results[index].Error = err.Error()
  48. }
  49. results[index].Fid = fid
  50. results[index].FileUrl = ret.PublicUrl + "/" + fid
  51. }
  52. return results, nil
  53. }
  54. func NewFileParts(fullPathFilenames []string) (ret []FilePart, err error) {
  55. ret = make([]FilePart, len(fullPathFilenames))
  56. for index, file := range fullPathFilenames {
  57. if ret[index], err = newFilePart(file); err != nil {
  58. return
  59. }
  60. }
  61. return
  62. }
  63. func newFilePart(fullPathFilename string) (ret FilePart, err error) {
  64. fh, openErr := os.Open(fullPathFilename)
  65. if openErr != nil {
  66. glog.V(0).Info("Failed to open file: ", fullPathFilename)
  67. return ret, openErr
  68. }
  69. ret.Reader = fh
  70. if fi, fiErr := fh.Stat(); fiErr != nil {
  71. glog.V(0).Info("Failed to stat file:", fullPathFilename)
  72. return ret, fiErr
  73. } else {
  74. ret.ModTime = fi.ModTime().UTC().Unix()
  75. ret.FileSize = fi.Size()
  76. }
  77. ext := strings.ToLower(path.Ext(fullPathFilename))
  78. ret.IsGzipped = ext == ".gz"
  79. if ret.IsGzipped {
  80. ret.FileName = fullPathFilename[0 : len(fullPathFilename)-3]
  81. }
  82. ret.FileName = fullPathFilename
  83. if ext != "" {
  84. ret.MimeType = mime.TypeByExtension(ext)
  85. }
  86. return ret, nil
  87. }
  88. func (fi FilePart) upload(server string, fid string, maxMB int, master, replication string) (retSize int, err error) {
  89. fileUrl := "http://" + server + "/" + fid
  90. if fi.ModTime != 0 {
  91. fileUrl += "?ts=" + strconv.Itoa(int(fi.ModTime))
  92. }
  93. if closer, ok := fi.Reader.(io.Closer); ok {
  94. defer closer.Close()
  95. }
  96. if maxMB > 0 && fi.FileSize > int64(maxMB*1024*1024) {
  97. chunkSize := int64(maxMB * 1024 * 1024)
  98. chunks := fi.FileSize/chunkSize + 1
  99. fids := make([]string, 0)
  100. for i := int64(0); i < chunks; i++ {
  101. id, count, e := upload_one_chunk(fi.FileName+"-"+strconv.FormatInt(i+1, 10), io.LimitReader(fi.Reader, chunkSize), master, replication)
  102. if e != nil {
  103. return 0, e
  104. }
  105. fids = append(fids, id)
  106. retSize += count
  107. }
  108. err = upload_file_id_list(fileUrl, fi.FileName+"-list", fids)
  109. } else {
  110. ret, e := Upload(fileUrl, fi.FileName, fi.Reader, fi.IsGzipped, fi.MimeType)
  111. if e != nil {
  112. return 0, e
  113. }
  114. return ret.Size, e
  115. }
  116. return
  117. }
  118. func upload_one_chunk(filename string, reader io.Reader, master, replication string) (fid string, size int, e error) {
  119. ret, err := Assign(master, 1, replication)
  120. if err != nil {
  121. return "", 0, err
  122. }
  123. fileUrl, fid := "http://"+ret.PublicUrl+"/"+ret.Fid, ret.Fid
  124. glog.V(4).Info("Uploading part ", filename, " to ", fileUrl, "...")
  125. uploadResult, uploadError := Upload(fileUrl, filename, reader, false, "application/octet-stream")
  126. if uploadError != nil {
  127. return fid, 0, uploadError
  128. }
  129. return fid, uploadResult.Size, nil
  130. }
  131. func upload_file_id_list(fileUrl, filename string, fids []string) error {
  132. var buf bytes.Buffer
  133. buf.WriteString(strings.Join(fids, "\n"))
  134. glog.V(4).Info("Uploading final list ", filename, " to ", fileUrl, "...")
  135. _, e := Upload(fileUrl, filename, &buf, false, "text/plain")
  136. return e
  137. }