@@ -408,6 +408,14 @@ func canReadFiles(r *context.Repository) bool {
408408 return r .Permission .CanRead (unit .TypeCode )
409409}
410410
411+ func base64Reader (s string ) (io.Reader , error ) {
412+ b , err := base64 .StdEncoding .DecodeString (s )
413+ if err != nil {
414+ return nil , err
415+ }
416+ return bytes .NewReader (b ), nil
417+ }
418+
411419// ChangeFiles handles API call for modifying multiple files
412420func ChangeFiles (ctx * context.APIContext ) {
413421 // swagger:operation POST /repos/{owner}/{repo}/contents repository repoChangeFiles
@@ -449,14 +457,19 @@ func ChangeFiles(ctx *context.APIContext) {
449457 apiOpts .BranchName = ctx .Repo .Repository .DefaultBranch
450458 }
451459
452- files := []* files_service.ChangeRepoFile {}
460+ var files []* files_service.ChangeRepoFile
453461 for _ , file := range apiOpts .Files {
462+ contentReader , err := base64Reader (file .ContentBase64 )
463+ if err != nil {
464+ ctx .Error (http .StatusUnprocessableEntity , "Invalid base64 content" , err )
465+ return
466+ }
454467 changeRepoFile := & files_service.ChangeRepoFile {
455- Operation : file .Operation ,
456- TreePath : file .Path ,
457- FromTreePath : file .FromPath ,
458- Content : file . Content ,
459- SHA : file .SHA ,
468+ Operation : file .Operation ,
469+ TreePath : file .Path ,
470+ FromTreePath : file .FromPath ,
471+ ContentReader : contentReader ,
472+ SHA : file .SHA ,
460473 }
461474 files = append (files , changeRepoFile )
462475 }
@@ -544,12 +557,18 @@ func CreateFile(ctx *context.APIContext) {
544557 apiOpts .BranchName = ctx .Repo .Repository .DefaultBranch
545558 }
546559
560+ contentReader , err := base64Reader (apiOpts .ContentBase64 )
561+ if err != nil {
562+ ctx .Error (http .StatusUnprocessableEntity , "Invalid base64 content" , err )
563+ return
564+ }
565+
547566 opts := & files_service.ChangeRepoFilesOptions {
548567 Files : []* files_service.ChangeRepoFile {
549568 {
550- Operation : "create" ,
551- TreePath : ctx .Params ("*" ),
552- Content : apiOpts . Content ,
569+ Operation : "create" ,
570+ TreePath : ctx .Params ("*" ),
571+ ContentReader : contentReader ,
553572 },
554573 },
555574 Message : apiOpts .Message ,
@@ -636,14 +655,20 @@ func UpdateFile(ctx *context.APIContext) {
636655 apiOpts .BranchName = ctx .Repo .Repository .DefaultBranch
637656 }
638657
658+ contentReader , err := base64Reader (apiOpts .ContentBase64 )
659+ if err != nil {
660+ ctx .Error (http .StatusUnprocessableEntity , "Invalid base64 content" , err )
661+ return
662+ }
663+
639664 opts := & files_service.ChangeRepoFilesOptions {
640665 Files : []* files_service.ChangeRepoFile {
641666 {
642- Operation : "update" ,
643- Content : apiOpts . Content ,
644- SHA : apiOpts .SHA ,
645- FromTreePath : apiOpts .FromPath ,
646- TreePath : ctx .Params ("*" ),
667+ Operation : "update" ,
668+ ContentReader : contentReader ,
669+ SHA : apiOpts .SHA ,
670+ FromTreePath : apiOpts .FromPath ,
671+ TreePath : ctx .Params ("*" ),
647672 },
648673 },
649674 Message : apiOpts .Message ,
@@ -709,14 +734,6 @@ func createOrUpdateFiles(ctx *context.APIContext, opts *files_service.ChangeRepo
709734 }
710735 }
711736
712- for _ , file := range opts .Files {
713- content , err := base64 .StdEncoding .DecodeString (file .Content )
714- if err != nil {
715- return nil , err
716- }
717- file .Content = string (content )
718- }
719-
720737 return files_service .ChangeRepoFiles (ctx , ctx .Repo .Repository , ctx .Doer , opts )
721738}
722739
0 commit comments