-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package search | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/ponzu-cms/ponzu/system/backup" | ||
) | ||
|
||
// Backup creates an archive of a project's search index and writes it | ||
// to the response as a download | ||
func Backup(ctx context.Context, res http.ResponseWriter) error { | ||
ts := time.Now().Unix() | ||
filename := fmt.Sprintf("search-%d.bak.tar.gz", ts) | ||
tmp := os.TempDir() | ||
bk := filepath.Join(tmp, filename) | ||
|
||
// create search-{stamp}.bak.tar.gz | ||
f, err := os.Create(bk) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
backup.ArchiveFS(ctx, "search", f) | ||
|
||
err = f.Close() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// write data to response | ||
data, err := os.Open(bk) | ||
if err != nil { | ||
return err | ||
} | ||
defer data.Close() | ||
defer os.Remove(bk) | ||
|
||
disposition := `attachment; filename=%s` | ||
info, err := data.Stat() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
res.Header().Set("Content-Type", "application/octet-stream") | ||
res.Header().Set("Content-Disposition", fmt.Sprintf(disposition, ts)) | ||
res.Header().Set("Content-Length", fmt.Sprintf("%d", info.Size())) | ||
|
||
_, err = io.Copy(res, data) | ||
|
||
return err | ||
} |