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.

377 lines
11 KiB

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