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.

189 lines
5.5 KiB

  1. package weed_server
  2. import (
  3. "bytes"
  4. "io"
  5. "io/ioutil"
  6. "net/http"
  7. "path"
  8. "strconv"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/filer2"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/operation"
  13. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  14. )
  15. func (fs *FilerServer) autoChunk(w http.ResponseWriter, r *http.Request, replication string, collection string) bool {
  16. if r.Method != "POST" {
  17. glog.V(4).Infoln("AutoChunking not supported for method", r.Method)
  18. return false
  19. }
  20. // autoChunking can be set at the command-line level or as a query param. Query param overrides command-line
  21. query := r.URL.Query()
  22. parsedMaxMB, _ := strconv.ParseInt(query.Get("maxMB"), 10, 32)
  23. maxMB := int32(parsedMaxMB)
  24. if maxMB <= 0 && fs.maxMB > 0 {
  25. maxMB = int32(fs.maxMB)
  26. }
  27. if maxMB <= 0 {
  28. glog.V(4).Infoln("AutoChunking not enabled")
  29. return false
  30. }
  31. glog.V(4).Infoln("AutoChunking level set to", maxMB, "(MB)")
  32. chunkSize := 1024 * 1024 * maxMB
  33. contentLength := int64(0)
  34. if contentLengthHeader := r.Header["Content-Length"]; len(contentLengthHeader) == 1 {
  35. contentLength, _ = strconv.ParseInt(contentLengthHeader[0], 10, 64)
  36. if contentLength <= int64(chunkSize) {
  37. glog.V(4).Infoln("Content-Length of", contentLength, "is less than the chunk size of", chunkSize, "so autoChunking will be skipped.")
  38. return false
  39. }
  40. }
  41. if contentLength <= 0 {
  42. glog.V(4).Infoln("Content-Length value is missing or unexpected so autoChunking will be skipped.")
  43. return false
  44. }
  45. reply, err := fs.doAutoChunk(w, r, contentLength, chunkSize, replication, collection)
  46. if err != nil {
  47. writeJsonError(w, r, http.StatusInternalServerError, err)
  48. } else if reply != nil {
  49. writeJsonQuiet(w, r, http.StatusCreated, reply)
  50. }
  51. return true
  52. }
  53. func (fs *FilerServer) doAutoChunk(w http.ResponseWriter, r *http.Request, contentLength int64, chunkSize int32, replication string, collection string) (filerResult *FilerPostResult, replyerr error) {
  54. multipartReader, multipartReaderErr := r.MultipartReader()
  55. if multipartReaderErr != nil {
  56. return nil, multipartReaderErr
  57. }
  58. part1, part1Err := multipartReader.NextPart()
  59. if part1Err != nil {
  60. return nil, part1Err
  61. }
  62. fileName := part1.FileName()
  63. if fileName != "" {
  64. fileName = path.Base(fileName)
  65. }
  66. var fileChunks []*filer_pb.FileChunk
  67. totalBytesRead := int64(0)
  68. tmpBufferSize := int32(1024 * 1024)
  69. tmpBuffer := bytes.NewBuffer(make([]byte, 0, tmpBufferSize))
  70. chunkBuf := make([]byte, chunkSize+tmpBufferSize, chunkSize+tmpBufferSize) // chunk size plus a little overflow
  71. chunkBufOffset := int32(0)
  72. chunkOffset := int64(0)
  73. writtenChunks := 0
  74. filerResult = &FilerPostResult{
  75. Name: fileName,
  76. }
  77. for totalBytesRead < contentLength {
  78. tmpBuffer.Reset()
  79. bytesRead, readErr := io.CopyN(tmpBuffer, part1, int64(tmpBufferSize))
  80. readFully := readErr != nil && readErr == io.EOF
  81. tmpBuf := tmpBuffer.Bytes()
  82. bytesToCopy := tmpBuf[0:int(bytesRead)]
  83. copy(chunkBuf[chunkBufOffset:chunkBufOffset+int32(bytesRead)], bytesToCopy)
  84. chunkBufOffset = chunkBufOffset + int32(bytesRead)
  85. if chunkBufOffset >= chunkSize || readFully || (chunkBufOffset > 0 && bytesRead == 0) {
  86. writtenChunks = writtenChunks + 1
  87. fileId, urlLocation, assignErr := fs.assignNewFileInfo(w, r, replication, collection)
  88. if assignErr != nil {
  89. return nil, assignErr
  90. }
  91. // upload the chunk to the volume server
  92. chunkName := fileName + "_chunk_" + strconv.FormatInt(int64(len(fileChunks)+1), 10)
  93. uploadErr := fs.doUpload(urlLocation, w, r, chunkBuf[0:chunkBufOffset], chunkName, "application/octet-stream", fileId)
  94. if uploadErr != nil {
  95. return nil, uploadErr
  96. }
  97. // Save to chunk manifest structure
  98. fileChunks = append(fileChunks,
  99. &filer_pb.FileChunk{
  100. FileId: fileId,
  101. Offset: chunkOffset,
  102. Size: uint64(chunkBufOffset),
  103. Mtime: time.Now().UnixNano(),
  104. },
  105. )
  106. // reset variables for the next chunk
  107. chunkBufOffset = 0
  108. chunkOffset = totalBytesRead + int64(bytesRead)
  109. }
  110. totalBytesRead = totalBytesRead + int64(bytesRead)
  111. if bytesRead == 0 || readFully {
  112. break
  113. }
  114. if readErr != nil {
  115. return nil, readErr
  116. }
  117. }
  118. path := r.URL.Path
  119. // also delete the old fid unless PUT operation
  120. if r.Method != "PUT" {
  121. if entry, err := fs.filer.FindEntry(filer2.FullPath(path)); err == nil {
  122. for _, chunk := range entry.Chunks {
  123. oldFid := chunk.FileId
  124. operation.DeleteFile(fs.filer.GetMaster(), oldFid, fs.jwt(oldFid))
  125. }
  126. } else if err != nil {
  127. glog.V(0).Infof("error %v occur when finding %s in filer store", err, path)
  128. }
  129. }
  130. glog.V(4).Infoln("saving", path)
  131. entry := &filer2.Entry{
  132. FullPath: filer2.FullPath(path),
  133. Attr: filer2.Attr{
  134. Mtime: time.Now(),
  135. Crtime: time.Now(),
  136. Mode: 0660,
  137. },
  138. Chunks: fileChunks,
  139. }
  140. if db_err := fs.filer.CreateEntry(entry); db_err != nil {
  141. replyerr = db_err
  142. filerResult.Error = db_err.Error()
  143. glog.V(0).Infof("failing to write %s to filer server : %v", path, db_err)
  144. return
  145. }
  146. return
  147. }
  148. func (fs *FilerServer) doUpload(urlLocation string, w http.ResponseWriter, r *http.Request, chunkBuf []byte, fileName string, contentType string, fileId string) (err error) {
  149. err = nil
  150. ioReader := ioutil.NopCloser(bytes.NewBuffer(chunkBuf))
  151. uploadResult, uploadError := operation.Upload(urlLocation, fileName, ioReader, false, contentType, nil, fs.jwt(fileId))
  152. if uploadResult != nil {
  153. glog.V(0).Infoln("Chunk upload result. Name:", uploadResult.Name, "Fid:", fileId, "Size:", uploadResult.Size)
  154. }
  155. if uploadError != nil {
  156. err = uploadError
  157. }
  158. return
  159. }