filer_server_handlers_write_autochunk.go 11 KB

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