Add GetExecutionDownloadLink function to Service

This commit is contained in:
Luis Eduardo Jeréz Girón
2024-07-24 09:22:02 -06:00
parent c32db66d30
commit 6e398baca7
2 changed files with 45 additions and 0 deletions
@@ -0,0 +1,33 @@
package executions
import (
"context"
"fmt"
"time"
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
"github.com/google/uuid"
)
func (s *Service) GetExecutionDownloadLink(
ctx context.Context, executionID uuid.UUID,
) (string, error) {
data, err := s.dbgen.ExecutionsServiceGetDownloadLinkData(
ctx, dbgen.ExecutionsServiceGetDownloadLinkDataParams{
ExecutionID: executionID,
DecryptionKey: *s.env.PBW_ENCRYPTION_KEY,
},
)
if err != nil {
return "", err
}
if !data.Path.Valid {
return "", fmt.Errorf("execution has no file associated")
}
return s.ints.S3Client.GetDownloadLink(
data.DecryptedAccessKey, data.DecryptedSecretKey, data.Region,
data.Endpoint, data.BucketName, data.Path.String, time.Hour*12,
)
}
@@ -0,0 +1,12 @@
-- name: ExecutionsServiceGetDownloadLinkData :one
SELECT
executions.path AS path,
destinations.bucket_name AS bucket_name,
destinations.region AS region,
destinations.endpoint AS endpoint,
pgp_sym_decrypt(destinations.access_key, sqlc.arg('decryption_key')::TEXT) AS decrypted_access_key,
pgp_sym_decrypt(destinations.secret_key, sqlc.arg('decryption_key')::TEXT) AS decrypted_secret_key
FROM executions
JOIN backups ON backups.id = executions.backup_id
JOIN destinations ON destinations.id = backups.destination_id
WHERE executions.id = @execution_id;