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.

379 lines
10 KiB

4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  6. "io"
  7. "net/http"
  8. "os"
  9. "path"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/seaweedfs/seaweedfs/weed/filer"
  14. "github.com/seaweedfs/seaweedfs/weed/glog"
  15. "github.com/seaweedfs/seaweedfs/weed/operation"
  16. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  17. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  18. "github.com/seaweedfs/seaweedfs/weed/util"
  19. )
  20. func (fs *FilerServer) autoChunk(ctx context.Context, w http.ResponseWriter, r *http.Request, contentLength int64, so *operation.StorageOption) {
  21. // autoChunking can be set at the command-line level or as a query param. Query param overrides command-line
  22. query := r.URL.Query()
  23. parsedMaxMB, _ := strconv.ParseInt(query.Get("maxMB"), 10, 32)
  24. maxMB := int32(parsedMaxMB)
  25. if maxMB <= 0 && fs.option.MaxMB > 0 {
  26. maxMB = int32(fs.option.MaxMB)
  27. }
  28. chunkSize := 1024 * 1024 * maxMB
  29. var reply *FilerPostResult
  30. var err error
  31. var md5bytes []byte
  32. if r.Method == "POST" {
  33. if r.Header.Get("Content-Type") == "" && strings.HasSuffix(r.URL.Path, "/") {
  34. reply, err = fs.mkdir(ctx, w, r)
  35. } else {
  36. reply, md5bytes, err = fs.doPostAutoChunk(ctx, w, r, chunkSize, contentLength, so)
  37. }
  38. } else {
  39. reply, md5bytes, err = fs.doPutAutoChunk(ctx, w, r, chunkSize, contentLength, so)
  40. }
  41. if err != nil {
  42. if strings.HasPrefix(err.Error(), "read input:") {
  43. writeJsonError(w, r, 499, err)
  44. } else if strings.HasSuffix(err.Error(), "is a file") {
  45. writeJsonError(w, r, http.StatusConflict, err)
  46. } else {
  47. writeJsonError(w, r, http.StatusInternalServerError, err)
  48. }
  49. } else if reply != nil {
  50. if len(md5bytes) > 0 {
  51. md5InBase64 := util.Base64Encode(md5bytes)
  52. w.Header().Set("Content-MD5", md5InBase64)
  53. }
  54. writeJsonQuiet(w, r, http.StatusCreated, reply)
  55. }
  56. }
  57. func (fs *FilerServer) doPostAutoChunk(ctx context.Context, w http.ResponseWriter, r *http.Request, chunkSize int32, contentLength int64, so *operation.StorageOption) (filerResult *FilerPostResult, md5bytes []byte, replyerr error) {
  58. multipartReader, multipartReaderErr := r.MultipartReader()
  59. if multipartReaderErr != nil {
  60. return nil, nil, multipartReaderErr
  61. }
  62. part1, part1Err := multipartReader.NextPart()
  63. if part1Err != nil {
  64. return nil, nil, part1Err
  65. }
  66. fileName := part1.FileName()
  67. if fileName != "" {
  68. fileName = path.Base(fileName)
  69. }
  70. contentType := part1.Header.Get("Content-Type")
  71. if contentType == "application/octet-stream" {
  72. contentType = ""
  73. }
  74. fileChunks, md5Hash, chunkOffset, err, smallContent := fs.uploadReaderToChunks(w, r, part1, chunkSize, fileName, contentType, contentLength, so)
  75. if err != nil {
  76. return nil, nil, err
  77. }
  78. md5bytes = md5Hash.Sum(nil)
  79. filerResult, replyerr = fs.saveMetaData(ctx, r, fileName, contentType, so, md5bytes, fileChunks, chunkOffset, smallContent)
  80. if replyerr != nil {
  81. fs.filer.DeleteChunks(fileChunks)
  82. }
  83. return
  84. }
  85. func (fs *FilerServer) doPutAutoChunk(ctx context.Context, w http.ResponseWriter, r *http.Request, chunkSize int32, contentLength int64, so *operation.StorageOption) (filerResult *FilerPostResult, md5bytes []byte, replyerr error) {
  86. fileName := path.Base(r.URL.Path)
  87. contentType := r.Header.Get("Content-Type")
  88. if contentType == "application/octet-stream" {
  89. contentType = ""
  90. }
  91. fileChunks, md5Hash, chunkOffset, err, smallContent := fs.uploadReaderToChunks(w, r, r.Body, chunkSize, fileName, contentType, contentLength, so)
  92. if err != nil {
  93. return nil, nil, err
  94. }
  95. md5bytes = md5Hash.Sum(nil)
  96. filerResult, replyerr = fs.saveMetaData(ctx, r, fileName, contentType, so, md5bytes, fileChunks, chunkOffset, smallContent)
  97. if replyerr != nil {
  98. fs.filer.DeleteChunks(fileChunks)
  99. }
  100. return
  101. }
  102. func isAppend(r *http.Request) bool {
  103. return r.URL.Query().Get("op") == "append"
  104. }
  105. func skipCheckParentDirEntry(r *http.Request) bool {
  106. return r.URL.Query().Get("skipCheckParentDir") == "true"
  107. }
  108. func (fs *FilerServer) saveMetaData(ctx context.Context, r *http.Request, fileName string, contentType string, so *operation.StorageOption, md5bytes []byte, fileChunks []*filer_pb.FileChunk, chunkOffset int64, content []byte) (filerResult *FilerPostResult, replyerr error) {
  109. // detect file mode
  110. modeStr := r.URL.Query().Get("mode")
  111. if modeStr == "" {
  112. modeStr = "0660"
  113. }
  114. mode, err := strconv.ParseUint(modeStr, 8, 32)
  115. if err != nil {
  116. glog.Errorf("Invalid mode format: %s, use 0660 by default", modeStr)
  117. mode = 0660
  118. }
  119. // fix the path
  120. path := r.URL.Path
  121. if strings.HasSuffix(path, "/") {
  122. if fileName != "" {
  123. path += fileName
  124. }
  125. } else {
  126. if fileName != "" {
  127. if possibleDirEntry, findDirErr := fs.filer.FindEntry(ctx, util.FullPath(path)); findDirErr == nil {
  128. if possibleDirEntry.IsDirectory() {
  129. path += "/" + fileName
  130. }
  131. }
  132. }
  133. }
  134. var entry *filer.Entry
  135. var newChunks []*filer_pb.FileChunk
  136. var mergedChunks []*filer_pb.FileChunk
  137. isAppend := isAppend(r)
  138. isOffsetWrite := len(fileChunks) > 0 && fileChunks[0].Offset > 0
  139. // when it is an append
  140. if isAppend || isOffsetWrite {
  141. existingEntry, findErr := fs.filer.FindEntry(ctx, util.FullPath(path))
  142. if findErr != nil && findErr != filer_pb.ErrNotFound {
  143. glog.V(0).Infof("failing to find %s: %v", path, findErr)
  144. }
  145. entry = existingEntry
  146. }
  147. if entry != nil {
  148. entry.Mtime = time.Now()
  149. entry.Md5 = nil
  150. // adjust chunk offsets
  151. if isAppend {
  152. for _, chunk := range fileChunks {
  153. chunk.Offset += int64(entry.FileSize)
  154. }
  155. entry.FileSize += uint64(chunkOffset)
  156. }
  157. newChunks = append(entry.Chunks, fileChunks...)
  158. // TODO
  159. if len(entry.Content) > 0 {
  160. replyerr = fmt.Errorf("append to small file is not supported yet")
  161. return
  162. }
  163. } else {
  164. glog.V(4).Infoln("saving", path)
  165. newChunks = fileChunks
  166. entry = &filer.Entry{
  167. FullPath: util.FullPath(path),
  168. Attr: filer.Attr{
  169. Mtime: time.Now(),
  170. Crtime: time.Now(),
  171. Mode: os.FileMode(mode),
  172. Uid: OS_UID,
  173. Gid: OS_GID,
  174. TtlSec: so.TtlSeconds,
  175. Mime: contentType,
  176. Md5: md5bytes,
  177. FileSize: uint64(chunkOffset),
  178. },
  179. Content: content,
  180. }
  181. }
  182. // maybe concatenate small chunks into one whole chunk
  183. mergedChunks, replyerr = fs.maybeMergeChunks(so, newChunks)
  184. if replyerr != nil {
  185. glog.V(0).Infof("merge chunks %s: %v", r.RequestURI, replyerr)
  186. mergedChunks = newChunks
  187. }
  188. // maybe compact entry chunks
  189. mergedChunks, replyerr = filer.MaybeManifestize(fs.saveAsChunk(so), mergedChunks)
  190. if replyerr != nil {
  191. glog.V(0).Infof("manifestize %s: %v", r.RequestURI, replyerr)
  192. return
  193. }
  194. entry.Chunks = mergedChunks
  195. if isOffsetWrite {
  196. entry.Md5 = nil
  197. entry.FileSize = entry.Size()
  198. }
  199. filerResult = &FilerPostResult{
  200. Name: fileName,
  201. Size: int64(entry.FileSize),
  202. }
  203. entry.Extended = SaveAmzMetaData(r, entry.Extended, false)
  204. for k, v := range r.Header {
  205. if len(v) > 0 && len(v[0]) > 0 {
  206. if strings.HasPrefix(k, needle.PairNamePrefix) || k == "Cache-Control" || k == "Expires" || k == "Content-Disposition" {
  207. entry.Extended[k] = []byte(v[0])
  208. }
  209. if k == "Response-Content-Disposition" {
  210. entry.Extended["Content-Disposition"] = []byte(v[0])
  211. }
  212. }
  213. }
  214. if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil, skipCheckParentDirEntry(r)); dbErr != nil {
  215. replyerr = dbErr
  216. filerResult.Error = dbErr.Error()
  217. glog.V(0).Infof("failing to write %s to filer server : %v", path, dbErr)
  218. }
  219. return filerResult, replyerr
  220. }
  221. func (fs *FilerServer) saveAsChunk(so *operation.StorageOption) filer.SaveDataAsChunkFunctionType {
  222. return func(reader io.Reader, name string, offset int64) (*filer_pb.FileChunk, error) {
  223. var fileId string
  224. var uploadResult *operation.UploadResult
  225. err := util.Retry("saveAsChunk", func() error {
  226. // assign one file id for one chunk
  227. assignedFileId, urlLocation, auth, assignErr := fs.assignNewFileInfo(so)
  228. if assignErr != nil {
  229. return assignErr
  230. }
  231. fileId = assignedFileId
  232. // upload the chunk to the volume server
  233. uploadOption := &operation.UploadOption{
  234. UploadUrl: urlLocation,
  235. Filename: name,
  236. Cipher: fs.option.Cipher,
  237. IsInputCompressed: false,
  238. MimeType: "",
  239. PairMap: nil,
  240. Jwt: auth,
  241. }
  242. var uploadErr error
  243. uploadResult, uploadErr, _ = operation.Upload(reader, uploadOption)
  244. if uploadErr != nil {
  245. return uploadErr
  246. }
  247. return nil
  248. })
  249. if err != nil {
  250. return nil, err
  251. }
  252. return uploadResult.ToPbFileChunk(fileId, offset), nil
  253. }
  254. }
  255. func (fs *FilerServer) mkdir(ctx context.Context, w http.ResponseWriter, r *http.Request) (filerResult *FilerPostResult, replyerr error) {
  256. // detect file mode
  257. modeStr := r.URL.Query().Get("mode")
  258. if modeStr == "" {
  259. modeStr = "0660"
  260. }
  261. mode, err := strconv.ParseUint(modeStr, 8, 32)
  262. if err != nil {
  263. glog.Errorf("Invalid mode format: %s, use 0660 by default", modeStr)
  264. mode = 0660
  265. }
  266. // fix the path
  267. path := r.URL.Path
  268. if strings.HasSuffix(path, "/") {
  269. path = path[:len(path)-1]
  270. }
  271. existingEntry, err := fs.filer.FindEntry(ctx, util.FullPath(path))
  272. if err == nil && existingEntry != nil {
  273. replyerr = fmt.Errorf("dir %s already exists", path)
  274. return
  275. }
  276. glog.V(4).Infoln("mkdir", path)
  277. entry := &filer.Entry{
  278. FullPath: util.FullPath(path),
  279. Attr: filer.Attr{
  280. Mtime: time.Now(),
  281. Crtime: time.Now(),
  282. Mode: os.FileMode(mode) | os.ModeDir,
  283. Uid: OS_UID,
  284. Gid: OS_GID,
  285. },
  286. }
  287. filerResult = &FilerPostResult{
  288. Name: util.FullPath(path).Name(),
  289. }
  290. if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil, false); dbErr != nil {
  291. replyerr = dbErr
  292. filerResult.Error = dbErr.Error()
  293. glog.V(0).Infof("failing to create dir %s on filer server : %v", path, dbErr)
  294. }
  295. return filerResult, replyerr
  296. }
  297. func SaveAmzMetaData(r *http.Request, existing map[string][]byte, isReplace bool) (metadata map[string][]byte) {
  298. metadata = make(map[string][]byte)
  299. if !isReplace {
  300. for k, v := range existing {
  301. metadata[k] = v
  302. }
  303. }
  304. if sc := r.Header.Get(s3_constants.AmzStorageClass); sc != "" {
  305. metadata[s3_constants.AmzStorageClass] = []byte(sc)
  306. }
  307. if tags := r.Header.Get(s3_constants.AmzObjectTagging); tags != "" {
  308. for _, v := range strings.Split(tags, "&") {
  309. tag := strings.Split(v, "=")
  310. if len(tag) == 2 {
  311. metadata[s3_constants.AmzObjectTagging+"-"+tag[0]] = []byte(tag[1])
  312. } else if len(tag) == 1 {
  313. metadata[s3_constants.AmzObjectTagging+"-"+tag[0]] = nil
  314. }
  315. }
  316. }
  317. for header, values := range r.Header {
  318. if strings.HasPrefix(header, s3_constants.AmzUserMetaPrefix) {
  319. for _, value := range values {
  320. metadata[header] = []byte(value)
  321. }
  322. }
  323. }
  324. return
  325. }