mirror of
https://github.com/1f349/site-hosting.git
synced 2025-02-23 06:34:58 +00:00
Use a separate work dir to keep the site available until the upload has been extracted
This commit is contained in:
parent
5f50ec5a56
commit
20de90afe8
@ -113,6 +113,7 @@ func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) er
|
|||||||
|
|
||||||
siteBranchPath := filepath.Join(site, "@"+branch)
|
siteBranchPath := filepath.Join(site, "@"+branch)
|
||||||
siteBranchOldPath := filepath.Join(site, "old@"+branch)
|
siteBranchOldPath := filepath.Join(site, "old@"+branch)
|
||||||
|
siteBranchWorkPath := filepath.Join(site, "work@"+branch)
|
||||||
|
|
||||||
// try the new "old@[...]" and old "@[...].old" paths
|
// try the new "old@[...]" and old "@[...].old" paths
|
||||||
err = h.storageFs.RemoveAll(siteBranchPath + ".old")
|
err = h.storageFs.RemoveAll(siteBranchPath + ".old")
|
||||||
@ -124,16 +125,11 @@ func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) er
|
|||||||
return fmt.Errorf("failed to remove old site branch %s: %w", siteBranchPath, err)
|
return fmt.Errorf("failed to remove old site branch %s: %w", siteBranchPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.storageFs.Rename(siteBranchPath, siteBranchOldPath)
|
err = h.storageFs.MkdirAll(siteBranchWorkPath, fs.ModePerm)
|
||||||
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
||||||
return fmt.Errorf("failed to save an old copy of the site: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = h.storageFs.MkdirAll(siteBranchPath, fs.ModePerm)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to make site directory: %w", err)
|
return fmt.Errorf("failed to make site directory: %w", err)
|
||||||
}
|
}
|
||||||
branchFs := afero.NewBasePathFs(h.storageFs, siteBranchPath)
|
branchFs := afero.NewBasePathFs(h.storageFs, siteBranchWorkPath)
|
||||||
|
|
||||||
// decompress gzip wrapper
|
// decompress gzip wrapper
|
||||||
gzipReader, err := gzip.NewReader(fileData)
|
gzipReader, err := gzip.NewReader(fileData)
|
||||||
@ -169,6 +165,18 @@ func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) er
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(melon): I would love to use unix.Renameat2 but due to afero this will not work
|
||||||
|
|
||||||
|
err = h.storageFs.Rename(siteBranchPath, siteBranchOldPath)
|
||||||
|
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||||
|
return fmt.Errorf("failed to save an old copy of the site: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = h.storageFs.Rename(siteBranchWorkPath, siteBranchPath)
|
||||||
|
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||||
|
return fmt.Errorf("failed to save an old copy of the site: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
n := time.Now().UTC()
|
n := time.Now().UTC()
|
||||||
|
|
||||||
err = h.db.AddBranch(context.Background(), database.AddBranchParams{
|
err = h.db.AddBranch(context.Background(), database.AddBranchParams{
|
||||||
|
@ -53,6 +53,9 @@ func assertUploadedFile(t *testing.T, fs afero.Fs, branch string) {
|
|||||||
assert.False(t, stat.IsDir())
|
assert.False(t, stat.IsDir())
|
||||||
assert.Equal(t, int64(13), stat.Size())
|
assert.Equal(t, int64(13), stat.Size())
|
||||||
|
|
||||||
|
stat, err = fs.Stat("example.com/work@" + branch + "/test.txt")
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
// check contents
|
// check contents
|
||||||
o, err := fs.Open("example.com/@" + branch + "/test.txt")
|
o, err := fs.Open("example.com/@" + branch + "/test.txt")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user