@@ -20,11 +20,10 @@ import (
2020 "compress/gzip"
2121 "fmt"
2222 "io"
23+ "io/fs"
2324 "mime/multipart"
2425 "net/http"
25- "os"
2626 "path"
27- "path/filepath"
2827 "strings"
2928
3029 "github.com/schollz/progressbar/v3"
@@ -54,17 +53,17 @@ var (
5453)
5554
5655func UploadTarball (
57- directory string ,
56+ directory fs. FS ,
5857 presignedUrl string ,
5958 presignedPostRequest * livekit.PresignedPostRequest ,
6059 excludeFiles []string ,
6160 projectType ProjectType ,
6261) error {
6362 excludeFiles = append (excludeFiles , defaultExcludePatterns ... )
6463
65- loadExcludeFiles := func (filename string ) (bool , string , error ) {
66- if _ , err := os .Stat (filename ); err == nil {
67- content , err := os .ReadFile (filename )
64+ loadExcludeFiles := func (dir fs. FS , filename string ) (bool , string , error ) {
65+ if _ , err := fs .Stat (dir , filename ); err == nil {
66+ content , err := fs .ReadFile (dir , filename )
6867 if err != nil {
6968 return false , "" , err
7069 }
@@ -75,7 +74,7 @@ func UploadTarball(
7574
7675 foundDockerIgnore := false
7776 for _ , exclude := range ignoreFilePatterns {
78- found , content , err := loadExcludeFiles (path . Join ( directory , exclude ) )
77+ found , content , err := loadExcludeFiles (directory , exclude )
7978 if err != nil {
8079 logger .Debugw ("failed to load exclude file" , "filename" , exclude , "error" , err )
8180 continue
@@ -89,7 +88,7 @@ func UploadTarball(
8988 // need to ensure we use a dockerignore file
9089 // if we fail to load a dockerignore file, we have to exit
9190 if ! foundDockerIgnore {
92- dockerIgnoreContent , err := fs .ReadFile (path .Join ("examples" , string (projectType )+ ".dockerignore" ))
91+ dockerIgnoreContent , err := embedfs .ReadFile (path .Join ("examples" , string (projectType )+ ".dockerignore" ))
9392 if err != nil {
9493 return fmt .Errorf ("failed to load exclude file %s: %w" , string (projectType ), err )
9594 }
@@ -105,14 +104,14 @@ func UploadTarball(
105104 excludeFiles [i ] = strings .TrimSpace (exclude )
106105 }
107106
108- checkFilesToInclude := func (path string , info os. FileInfo ) bool {
109- fileName := filepath .Base (path )
107+ checkFilesToInclude := func (p string ) bool {
108+ fileName := path .Base (p )
110109 // we have to include the Dockerfile in the upload, as it is required for the build
111110 if strings .Contains (fileName , "Dockerfile" ) {
112111 return true
113112 }
114113
115- if ignored , err := matcher .MatchesOrParentMatches (path ); ignored {
114+ if ignored , err := matcher .MatchesOrParentMatches (p ); ignored {
116115 return false
117116 } else if err != nil {
118117 return false
@@ -123,17 +122,17 @@ func UploadTarball(
123122 // we walk the directory first to calculate the total size of the tarball
124123 // this lets the progress bar show the correct progress
125124 var totalSize int64
126- err = filepath . Walk (directory , func (path string , info os. FileInfo , err error ) error {
125+ err = fs . WalkDir (directory , "." , func (path string , d fs. DirEntry , err error ) error {
127126 if err != nil {
128127 return err
129128 }
130129
131- relPath , err := filepath . Rel ( directory , path )
130+ info , err := d . Info ( )
132131 if err != nil {
133- return nil
132+ return err
134133 }
135134
136- if ! checkFilesToInclude (relPath , info ) {
135+ if ! checkFilesToInclude (path ) {
137136 return nil
138137 }
139138
@@ -166,62 +165,28 @@ func UploadTarball(
166165 tarWriter := tar .NewWriter (gzipWriter )
167166 defer tarWriter .Close ()
168167
169- err = filepath . Walk (directory , func (path string , info os. FileInfo , err error ) error {
168+ err = fs . WalkDir (directory , "." , func (path string , d fs. DirEntry , err error ) error {
170169 if err != nil {
171170 return err
172171 }
173172
174- relPath , err := filepath . Rel ( directory , path )
173+ info , err := d . Info ( )
175174 if err != nil {
176- return fmt . Errorf ( "failed to calculate relative path for %s: %w" , path , err )
175+ return err
177176 }
178177
179- if ! checkFilesToInclude (relPath , info ) {
178+ if ! checkFilesToInclude (path ) {
180179 logger .Debugw ("excluding file from tarball" , "path" , path )
181180 return nil
182181 }
183182
184- // Follow symlinks and include the actual file contents
185- if info .Mode ()& os .ModeSymlink != 0 {
186- realPath , err := filepath .EvalSymlinks (path )
187- if err != nil {
188- return fmt .Errorf ("failed to evaluate symlink %s: %w" , path , err )
189- }
190- info , err = os .Stat (realPath )
191- if err != nil {
192- return fmt .Errorf ("failed to stat %s: %w" , realPath , err )
193- }
194- // Open the real file instead of the symlink
195- file , err := os .Open (realPath )
196- if err != nil {
197- return fmt .Errorf ("failed to open file %s: %w" , realPath , err )
198- }
199- defer file .Close ()
200-
201- header , err := tar .FileInfoHeader (info , "" )
202- if err != nil {
203- return fmt .Errorf ("failed to create tar header for file %s: %w" , path , err )
204- }
205- header .Name = relPath
206- if err := tarWriter .WriteHeader (header ); err != nil {
207- return fmt .Errorf ("failed to write tar header for file %s: %w" , path , err )
208- }
209-
210- // Copy file contents directly without progress bar
211- _ , err = io .Copy (tarWriter , file )
212- if err != nil {
213- return fmt .Errorf ("failed to copy file content for %s: %w" , path , err )
214- }
215- return nil
216- }
217-
218183 // Handle directories
219184 if info .IsDir () {
220185 header , err := tar .FileInfoHeader (info , "" )
221186 if err != nil {
222187 return fmt .Errorf ("failed to create tar header for directory %s: %w" , path , err )
223188 }
224- header .Name = relPath + "/"
189+ header .Name = util . ToUnixPath ( path ) + "/"
225190 if err := tarWriter .WriteHeader (header ); err != nil {
226191 return fmt .Errorf ("failed to write tar header for directory %s: %w" , path , err )
227192 }
@@ -234,7 +199,7 @@ func UploadTarball(
234199 return nil
235200 }
236201
237- file , err := os .Open (path )
202+ file , err := directory .Open (path )
238203 if err != nil {
239204 return fmt .Errorf ("failed to open file %s: %w" , path , err )
240205 }
@@ -244,7 +209,7 @@ func UploadTarball(
244209 if err != nil {
245210 return fmt .Errorf ("failed to create tar header for file %s: %w" , path , err )
246211 }
247- header .Name = util .ToUnixPath (relPath )
212+ header .Name = util .ToUnixPath (path )
248213 if err := tarWriter .WriteHeader (header ); err != nil {
249214 return fmt .Errorf ("failed to write tar header for file %s: %w" , path , err )
250215 }
0 commit comments