Skip to content

Commit 96f48b1

Browse files
authored
Merge pull request #4002 from uselagoon/get-file-backup-resolvers
feat: get backup by backup id and file by file id resolver
2 parents e5a91c9 + 8377cdc commit 96f48b1

File tree

5 files changed

+64
-10
lines changed

5 files changed

+64
-10
lines changed

services/api/src/resolvers.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const {
9898
uploadFilesForTask,
9999
deleteFilesForTask,
100100
getDownloadLink,
101+
getDownloadLinkByTaskFileId,
101102
} = require('./resources/file/resolvers');
102103

103104
const {
@@ -283,6 +284,7 @@ const {
283284
addRestore,
284285
getRestoreByBackupId,
285286
updateRestore,
287+
getBackupDownloadLinkByBackupId,
286288
backupSubscriber,
287289
getRestoreLocation,
288290
} = require('./resources/backup/resolvers');
@@ -683,7 +685,9 @@ async function getResolvers() {
683685
checkBulkImportProjectsAndGroupsToOrganization,
684686
allPlatformUsers: getAllPlatformUsers,
685687
getAuditLogs,
686-
listAllRetentionPolicies
688+
listAllRetentionPolicies,
689+
getBackupDownloadLinkByBackupId,
690+
getDownloadLinkByTaskFileId,
687691
},
688692
Mutation: {
689693
addProblem,

services/api/src/resources/backup/resolvers.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,31 @@ export const updateRestore: ResolverFn = async (
498498
return restoreData;
499499
};
500500

501+
export const getBackupDownloadLinkByBackupId: ResolverFn = async (
502+
root,
503+
{ backupId },
504+
{ sqlClientPool, hasPermission, userActivityLogger, adminScopes }
505+
) => {
506+
const perms = await query(sqlClientPool, Sql.selectPermsForBackup(backupId));
507+
508+
await hasPermission('backup', 'view', {
509+
project: R.path(['0', 'pid'], perms)
510+
});
511+
512+
const rows = await query(
513+
sqlClientPool,
514+
Sql.selectRestoreByBackupId(backupId)
515+
);
516+
const row = R.prop(0, rows)
517+
518+
if (!row || row.restoreLocation == null) {
519+
throw new Error(`no restore file available`);
520+
}
521+
const backupData = R.prop(0, rows);
522+
const [restLoc, restSize] = await getRestoreLocation(backupId, backupData.restoreLocation, sqlClientPool, userActivityLogger);
523+
return restLoc;
524+
}
525+
501526
export const getRestoreByBackupId: ResolverFn = async (
502527
{ backupId },
503528
args,

services/api/src/resources/file/resolvers.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@ const { Upload } = require('@aws-sdk/lib-storage');
1616
const isGCS = process.env.S3_FILES_GCS || 'false'
1717
const bucket = process.env.S3_FILES_BUCKET || 'lagoon-files'
1818

19-
20-
export const getDownloadLink: ResolverFn = async ({ s3Key }, input, { userActivityLogger }) => {
21-
const command = new GetObjectCommand({
22-
Bucket: bucket,
23-
Key: s3Key,
24-
});
19+
async function generateDownloadLink(s3Key: string, userActivityLogger?: Function ) {
20+
const command = new GetObjectCommand({ Bucket: bucket, Key: s3Key });
2521

2622
if (typeof userActivityLogger === 'function') {
27-
2823
const auditLog: AuditLog = {
2924
resource: {
3025
type: AuditType.FILE,
@@ -40,12 +35,33 @@ export const getDownloadLink: ResolverFn = async ({ s3Key }, input, { userActivi
4035
}
4136
});
4237
}
43-
44-
return getSignedUrl(s3Client, command, {
38+
const signedUrl = await getSignedUrl(s3Client, command, {
4539
expiresIn: s3Config.signedLinkExpiration,
4640
});
41+
return signedUrl;
42+
}
43+
44+
export const getDownloadLink: ResolverFn = async ({ s3Key }, input, { userActivityLogger }) => {
45+
return generateDownloadLink(s3Key, userActivityLogger);
4746
};
4847

48+
export const getDownloadLinkByTaskFileId: ResolverFn = async (
49+
root,
50+
{ taskId, fileId },
51+
{ sqlClientPool, hasPermission, userActivityLogger }
52+
) => {
53+
const rowsPerms = await query(sqlClientPool, taskSql.selectPermsForTask(taskId));
54+
55+
await hasPermission('task', 'view', {
56+
project: R.path(['0', 'pid'], rowsPerms)
57+
});
58+
59+
const rows = await query(sqlClientPool, Sql.selectTaskFileById(taskId, fileId));
60+
61+
const downloadUrl = await generateDownloadLink(rows[0].s3Key, userActivityLogger)
62+
return downloadUrl;
63+
}
64+
4965
export const getFilesByTaskId: ResolverFn = async (
5066
{ id: tid },
5167
_args,

services/api/src/resources/file/sql.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ export const Sql = {
88
.andWhere('s3_file.deleted', '0000-00-00 00:00:00')
99
.orderBy('s3_file.created', 'desc')
1010
.toString(),
11+
selectTaskFileById: (tid: number, fid: number) =>
12+
knex('task_file')
13+
.join('s3_file', 'task_file.fid', '=', 's3_file.id')
14+
.where('task_file.tid', tid)
15+
.andWhere('task_file.fid', fid)
16+
.andWhere('s3_file.deleted', '0000-00-00 00:00:00')
17+
.toString(),
1118
insertFile: ({
1219
id,
1320
filename,

services/api/src/typeDefs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,8 @@ const typeDefs = gql`
15221522
allPlatformUsers(id: String, email: String, gitlabId: Int, role: PlatformRole): [User]
15231523
getAuditLogs(input: AuditLogInput): [AuditLog]
15241524
listAllRetentionPolicies(name: String, type: RetentionPolicyType): [RetentionPolicy]
1525+
getBackupDownloadLinkByBackupId(backupId: String!): String
1526+
getDownloadLinkByTaskFileId(taskId: Int!, fileId: Int!): String
15251527
}
15261528
15271529
type ProjectGroupsToOrganization {

0 commit comments

Comments
 (0)