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.

file.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "encoding/base64"
  8. "net/http"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/repofiles"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/routers/repo"
  15. )
  16. // GetRawFile get a file by path on a repository
  17. func GetRawFile(ctx *context.APIContext) {
  18. // swagger:operation GET /repos/{owner}/{repo}/raw/{filepath} repository repoGetRawFile
  19. // ---
  20. // summary: Get a file from a repository
  21. // produces:
  22. // - application/json
  23. // parameters:
  24. // - name: owner
  25. // in: path
  26. // description: owner of the repo
  27. // type: string
  28. // required: true
  29. // - name: repo
  30. // in: path
  31. // description: name of the repo
  32. // type: string
  33. // required: true
  34. // - name: filepath
  35. // in: path
  36. // description: filepath of the file to get
  37. // type: string
  38. // required: true
  39. // responses:
  40. // 200:
  41. // description: success
  42. if ctx.Repo.Repository.IsEmpty {
  43. ctx.NotFound()
  44. return
  45. }
  46. blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
  47. if err != nil {
  48. if git.IsErrNotExist(err) {
  49. ctx.NotFound()
  50. } else {
  51. ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err)
  52. }
  53. return
  54. }
  55. if err = repo.ServeBlob(ctx.Context, blob); err != nil {
  56. ctx.Error(http.StatusInternalServerError, "ServeBlob", err)
  57. }
  58. }
  59. // GetArchive get archive of a repository
  60. func GetArchive(ctx *context.APIContext) {
  61. // swagger:operation GET /repos/{owner}/{repo}/archive/{archive} repository repoGetArchive
  62. // ---
  63. // summary: Get an archive of a repository
  64. // produces:
  65. // - application/json
  66. // parameters:
  67. // - name: owner
  68. // in: path
  69. // description: owner of the repo
  70. // type: string
  71. // required: true
  72. // - name: repo
  73. // in: path
  74. // description: name of the repo
  75. // type: string
  76. // required: true
  77. // - name: archive
  78. // in: path
  79. // description: archive to download, consisting of a git reference and archive
  80. // type: string
  81. // required: true
  82. // responses:
  83. // 200:
  84. // description: success
  85. repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
  86. gitRepo, err := git.OpenRepository(repoPath)
  87. if err != nil {
  88. ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
  89. return
  90. }
  91. ctx.Repo.GitRepo = gitRepo
  92. defer gitRepo.Close()
  93. repo.Download(ctx.Context)
  94. }
  95. // GetEditorconfig get editor config of a repository
  96. func GetEditorconfig(ctx *context.APIContext) {
  97. // swagger:operation GET /repos/{owner}/{repo}/editorconfig/{filepath} repository repoGetEditorConfig
  98. // ---
  99. // summary: Get the EditorConfig definitions of a file in a repository
  100. // produces:
  101. // - application/json
  102. // parameters:
  103. // - name: owner
  104. // in: path
  105. // description: owner of the repo
  106. // type: string
  107. // required: true
  108. // - name: repo
  109. // in: path
  110. // description: name of the repo
  111. // type: string
  112. // required: true
  113. // - name: filepath
  114. // in: path
  115. // description: filepath of file to get
  116. // type: string
  117. // required: true
  118. // responses:
  119. // 200:
  120. // description: success
  121. ec, err := ctx.Repo.GetEditorconfig()
  122. if err != nil {
  123. if git.IsErrNotExist(err) {
  124. ctx.NotFound(err)
  125. } else {
  126. ctx.Error(http.StatusInternalServerError, "GetEditorconfig", err)
  127. }
  128. return
  129. }
  130. fileName := ctx.Params("filename")
  131. def := ec.GetDefinitionForFilename(fileName)
  132. if def == nil {
  133. ctx.NotFound(err)
  134. return
  135. }
  136. ctx.JSON(http.StatusOK, def)
  137. }
  138. // CanWriteFiles returns true if repository is editable and user has proper access level.
  139. func CanWriteFiles(r *context.Repository) bool {
  140. return r.Permission.CanWrite(models.UnitTypeCode) && !r.Repository.IsMirror && !r.Repository.IsArchived
  141. }
  142. // CanReadFiles returns true if repository is readable and user has proper access level.
  143. func CanReadFiles(r *context.Repository) bool {
  144. return r.Permission.CanRead(models.UnitTypeCode)
  145. }
  146. // CreateFile handles API call for creating a file
  147. func CreateFile(ctx *context.APIContext, apiOpts api.CreateFileOptions) {
  148. // swagger:operation POST /repos/{owner}/{repo}/contents/{filepath} repository repoCreateFile
  149. // ---
  150. // summary: Create a file in a repository
  151. // consumes:
  152. // - application/json
  153. // produces:
  154. // - application/json
  155. // parameters:
  156. // - name: owner
  157. // in: path
  158. // description: owner of the repo
  159. // type: string
  160. // required: true
  161. // - name: repo
  162. // in: path
  163. // description: name of the repo
  164. // type: string
  165. // required: true
  166. // - name: filepath
  167. // in: path
  168. // description: path of the file to create
  169. // type: string
  170. // required: true
  171. // - name: body
  172. // in: body
  173. // required: true
  174. // schema:
  175. // "$ref": "#/definitions/CreateFileOptions"
  176. // responses:
  177. // "201":
  178. // "$ref": "#/responses/FileResponse"
  179. opts := &repofiles.UpdateRepoFileOptions{
  180. Content: apiOpts.Content,
  181. IsNewFile: true,
  182. Message: apiOpts.Message,
  183. TreePath: ctx.Params("*"),
  184. OldBranch: apiOpts.BranchName,
  185. NewBranch: apiOpts.NewBranchName,
  186. Committer: &repofiles.IdentityOptions{
  187. Name: apiOpts.Committer.Name,
  188. Email: apiOpts.Committer.Email,
  189. },
  190. Author: &repofiles.IdentityOptions{
  191. Name: apiOpts.Author.Name,
  192. Email: apiOpts.Author.Email,
  193. },
  194. }
  195. if opts.Message == "" {
  196. opts.Message = ctx.Tr("repo.editor.add", opts.TreePath)
  197. }
  198. if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
  199. ctx.Error(http.StatusInternalServerError, "CreateFile", err)
  200. } else {
  201. ctx.JSON(http.StatusCreated, fileResponse)
  202. }
  203. }
  204. // UpdateFile handles API call for updating a file
  205. func UpdateFile(ctx *context.APIContext, apiOpts api.UpdateFileOptions) {
  206. // swagger:operation PUT /repos/{owner}/{repo}/contents/{filepath} repository repoUpdateFile
  207. // ---
  208. // summary: Update a file in a repository
  209. // consumes:
  210. // - application/json
  211. // produces:
  212. // - application/json
  213. // parameters:
  214. // - name: owner
  215. // in: path
  216. // description: owner of the repo
  217. // type: string
  218. // required: true
  219. // - name: repo
  220. // in: path
  221. // description: name of the repo
  222. // type: string
  223. // required: true
  224. // - name: filepath
  225. // in: path
  226. // description: path of the file to update
  227. // type: string
  228. // required: true
  229. // - name: body
  230. // in: body
  231. // required: true
  232. // schema:
  233. // "$ref": "#/definitions/UpdateFileOptions"
  234. // responses:
  235. // "200":
  236. // "$ref": "#/responses/FileResponse"
  237. opts := &repofiles.UpdateRepoFileOptions{
  238. Content: apiOpts.Content,
  239. SHA: apiOpts.SHA,
  240. IsNewFile: false,
  241. Message: apiOpts.Message,
  242. FromTreePath: apiOpts.FromPath,
  243. TreePath: ctx.Params("*"),
  244. OldBranch: apiOpts.BranchName,
  245. NewBranch: apiOpts.NewBranchName,
  246. Committer: &repofiles.IdentityOptions{
  247. Name: apiOpts.Committer.Name,
  248. Email: apiOpts.Committer.Email,
  249. },
  250. Author: &repofiles.IdentityOptions{
  251. Name: apiOpts.Author.Name,
  252. Email: apiOpts.Author.Email,
  253. },
  254. }
  255. if opts.Message == "" {
  256. opts.Message = ctx.Tr("repo.editor.update", opts.TreePath)
  257. }
  258. if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
  259. ctx.Error(http.StatusInternalServerError, "UpdateFile", err)
  260. } else {
  261. ctx.JSON(http.StatusOK, fileResponse)
  262. }
  263. }
  264. // Called from both CreateFile or UpdateFile to handle both
  265. func createOrUpdateFile(ctx *context.APIContext, opts *repofiles.UpdateRepoFileOptions) (*api.FileResponse, error) {
  266. if !CanWriteFiles(ctx.Repo) {
  267. return nil, models.ErrUserDoesNotHaveAccessToRepo{
  268. UserID: ctx.User.ID,
  269. RepoName: ctx.Repo.Repository.LowerName,
  270. }
  271. }
  272. content, err := base64.StdEncoding.DecodeString(opts.Content)
  273. if err != nil {
  274. return nil, err
  275. }
  276. opts.Content = string(content)
  277. return repofiles.CreateOrUpdateRepoFile(ctx.Repo.Repository, ctx.User, opts)
  278. }
  279. // DeleteFile Delete a fle in a repository
  280. func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) {
  281. // swagger:operation DELETE /repos/{owner}/{repo}/contents/{filepath} repository repoDeleteFile
  282. // ---
  283. // summary: Delete a file in a repository
  284. // consumes:
  285. // - application/json
  286. // produces:
  287. // - application/json
  288. // parameters:
  289. // - name: owner
  290. // in: path
  291. // description: owner of the repo
  292. // type: string
  293. // required: true
  294. // - name: repo
  295. // in: path
  296. // description: name of the repo
  297. // type: string
  298. // required: true
  299. // - name: filepath
  300. // in: path
  301. // description: path of the file to delete
  302. // type: string
  303. // required: true
  304. // - name: body
  305. // in: body
  306. // required: true
  307. // schema:
  308. // "$ref": "#/definitions/DeleteFileOptions"
  309. // responses:
  310. // "200":
  311. // "$ref": "#/responses/FileDeleteResponse"
  312. if !CanWriteFiles(ctx.Repo) {
  313. ctx.Error(http.StatusInternalServerError, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{
  314. UserID: ctx.User.ID,
  315. RepoName: ctx.Repo.Repository.LowerName,
  316. })
  317. return
  318. }
  319. opts := &repofiles.DeleteRepoFileOptions{
  320. Message: apiOpts.Message,
  321. OldBranch: apiOpts.BranchName,
  322. NewBranch: apiOpts.NewBranchName,
  323. SHA: apiOpts.SHA,
  324. TreePath: ctx.Params("*"),
  325. Committer: &repofiles.IdentityOptions{
  326. Name: apiOpts.Committer.Name,
  327. Email: apiOpts.Committer.Email,
  328. },
  329. Author: &repofiles.IdentityOptions{
  330. Name: apiOpts.Author.Name,
  331. Email: apiOpts.Author.Email,
  332. },
  333. }
  334. if opts.Message == "" {
  335. opts.Message = ctx.Tr("repo.editor.delete", opts.TreePath)
  336. }
  337. if fileResponse, err := repofiles.DeleteRepoFile(ctx.Repo.Repository, ctx.User, opts); err != nil {
  338. ctx.Error(http.StatusInternalServerError, "DeleteFile", err)
  339. } else {
  340. ctx.JSON(http.StatusOK, fileResponse)
  341. }
  342. }
  343. // GetContents Get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
  344. func GetContents(ctx *context.APIContext) {
  345. // swagger:operation GET /repos/{owner}/{repo}/contents/{filepath} repository repoGetContents
  346. // ---
  347. // summary: Gets the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
  348. // produces:
  349. // - application/json
  350. // parameters:
  351. // - name: owner
  352. // in: path
  353. // description: owner of the repo
  354. // type: string
  355. // required: true
  356. // - name: repo
  357. // in: path
  358. // description: name of the repo
  359. // type: string
  360. // required: true
  361. // - name: filepath
  362. // in: path
  363. // description: path of the dir, file, symlink or submodule in the repo
  364. // type: string
  365. // required: true
  366. // - name: ref
  367. // in: query
  368. // description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
  369. // type: string
  370. // required: false
  371. // responses:
  372. // "200":
  373. // "$ref": "#/responses/ContentsResponse"
  374. if !CanReadFiles(ctx.Repo) {
  375. ctx.Error(http.StatusInternalServerError, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{
  376. UserID: ctx.User.ID,
  377. RepoName: ctx.Repo.Repository.LowerName,
  378. })
  379. return
  380. }
  381. treePath := ctx.Params("*")
  382. ref := ctx.QueryTrim("ref")
  383. if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil {
  384. ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
  385. } else {
  386. ctx.JSON(http.StatusOK, fileList)
  387. }
  388. }
  389. // GetContentsList Get the metadata of all the entries of the root dir
  390. func GetContentsList(ctx *context.APIContext) {
  391. // swagger:operation GET /repos/{owner}/{repo}/contents repository repoGetContentsList
  392. // ---
  393. // summary: Gets the metadata of all the entries of the root dir
  394. // produces:
  395. // - application/json
  396. // parameters:
  397. // - name: owner
  398. // in: path
  399. // description: owner of the repo
  400. // type: string
  401. // required: true
  402. // - name: repo
  403. // in: path
  404. // description: name of the repo
  405. // type: string
  406. // required: true
  407. // - name: ref
  408. // in: query
  409. // description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
  410. // type: string
  411. // required: false
  412. // responses:
  413. // "200":
  414. // "$ref": "#/responses/ContentsListResponse"
  415. // same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
  416. GetContents(ctx)
  417. }