2020-04-14 05:01:07 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-12-06 22:23:44 +00:00
|
|
|
"github.com/patrickmn/go-cache"
|
2020-10-31 23:47:41 +00:00
|
|
|
"strconv"
|
2020-12-06 22:23:44 +00:00
|
|
|
"time"
|
2020-12-06 20:56:17 +00:00
|
|
|
)
|
|
|
|
|
2020-12-06 22:23:44 +00:00
|
|
|
// backupUploadIDs stores a cache of active S3 backups.
|
2020-12-06 23:15:54 +00:00
|
|
|
var backupUploadIDs = cache.New(time.Hour*3, time.Minute*5)
|
2020-04-14 05:01:07 +00:00
|
|
|
|
2020-10-31 23:47:41 +00:00
|
|
|
type BackupRemoteUploadResponse struct {
|
2020-12-06 20:56:17 +00:00
|
|
|
UploadID string `json:"upload_id"`
|
|
|
|
Parts []string `json:"parts"`
|
|
|
|
PartSize int64 `json:"part_size"`
|
2020-10-31 23:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetBackupRemoteUploadURLs(backup string, size int64) (*BackupRemoteUploadResponse, error) {
|
|
|
|
resp, err := r.Get(fmt.Sprintf("/backups/%s", backup), Q{"size": strconv.FormatInt(size, 10)})
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, err
|
2020-10-31 23:47:41 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.HasError() {
|
|
|
|
return nil, resp.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
var res BackupRemoteUploadResponse
|
|
|
|
if err := resp.Bind(&res); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, err
|
2020-10-31 23:47:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 20:56:17 +00:00
|
|
|
// Store the backup upload id for later use, this is a janky way to be able to use it later with SendBackupStatus.
|
2020-12-06 22:23:44 +00:00
|
|
|
// Yes, the timeout of 3 hours is intentional, if this value is removed before the backup completes,
|
|
|
|
// the backup will fail even if it uploaded properly.
|
2020-12-06 23:15:54 +00:00
|
|
|
backupUploadIDs.Set(backup, res.UploadID, 0)
|
2020-12-06 20:56:17 +00:00
|
|
|
|
2020-10-31 23:47:41 +00:00
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
2020-04-14 05:01:07 +00:00
|
|
|
type BackupRequest struct {
|
2020-12-06 20:56:17 +00:00
|
|
|
UploadID string `json:"upload_id"`
|
2020-08-24 00:52:46 +00:00
|
|
|
Checksum string `json:"checksum"`
|
|
|
|
ChecksumType string `json:"checksum_type"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
Successful bool `json:"successful"`
|
2020-04-14 05:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notifies the panel that a specific backup has been completed and is now
|
|
|
|
// available for a user to view and download.
|
2020-10-31 17:04:20 +00:00
|
|
|
func (r *Request) SendBackupStatus(backup string, data BackupRequest) error {
|
2020-12-06 20:56:17 +00:00
|
|
|
// Set the UploadID on the data.
|
2020-12-06 22:23:44 +00:00
|
|
|
if v, ok := backupUploadIDs.Get(backup); ok {
|
|
|
|
data.UploadID = v.(string)
|
2020-12-06 20:56:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 22:04:57 +00:00
|
|
|
resp, err := r.Post(fmt.Sprintf("/backups/%s", backup), data)
|
2020-04-14 05:01:07 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-04-14 05:01:07 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-10-31 17:04:20 +00:00
|
|
|
return resp.Error()
|
2020-04-14 05:01:07 +00:00
|
|
|
}
|