You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

file.go 13 kB

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