@cirrobio/sdk 0.2.6 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AWSCredentials, FileApi } from "@cirrobio/api-client";
|
|
2
2
|
import { ProjectFileAccessContext } from "./project-access-context";
|
|
3
3
|
import { DownloadableFile } from "./file-object.model";
|
|
4
|
-
import {
|
|
4
|
+
import { GetFileUrlParams, GetSignedUrlOptions } from "./actions/sign-url.fn";
|
|
5
5
|
/**
|
|
6
6
|
* Service for viewing files in Cirro
|
|
7
7
|
* currently this only operates on files within a project
|
|
@@ -50,22 +50,28 @@ class FileService {
|
|
|
50
50
|
* Get credentials for accessing a project file
|
|
51
51
|
*/
|
|
52
52
|
async getProjectAccessCredentials(fileAccessContext) {
|
|
53
|
+
const accessType = fileAccessContext.fileAccessRequest.accessType;
|
|
53
54
|
// Special case for project download, since we can cache the credentials
|
|
54
|
-
if (
|
|
55
|
+
if (accessType === api_client_1.AccessType.ProjectDownload || accessType === api_client_1.AccessType.SharedDatasetDownload) {
|
|
55
56
|
return this.getProjectReadCredentials(fileAccessContext);
|
|
56
57
|
}
|
|
57
58
|
return this.fileApi.generateProjectFileAccessToken({ projectId: fileAccessContext.project.id, fileAccessRequest: fileAccessContext.fileAccessRequest });
|
|
58
59
|
}
|
|
59
60
|
async getProjectReadCredentials(fileAccessContext) {
|
|
60
61
|
const projectId = fileAccessContext.project.id;
|
|
62
|
+
// Append datasetId to cache key for shared dataset downloads since we need to generate a new token for each dataset
|
|
63
|
+
let cacheKey = projectId;
|
|
64
|
+
if (fileAccessContext.fileAccessRequest.accessType === api_client_1.AccessType.SharedDatasetDownload) {
|
|
65
|
+
cacheKey = `${projectId}-${fileAccessContext.fileAccessRequest.datasetId}`;
|
|
66
|
+
}
|
|
61
67
|
return credentials_mutex_so_1.credentialsMutex.dispatch(async () => {
|
|
62
|
-
const cachedCredentials = credentials_mutex_so_1.credentialsCache.get(
|
|
68
|
+
const cachedCredentials = credentials_mutex_so_1.credentialsCache.get(cacheKey);
|
|
63
69
|
const expirationTime = cachedCredentials ? cachedCredentials === null || cachedCredentials === void 0 ? void 0 : cachedCredentials.expiration : null;
|
|
64
70
|
const fetchNewCredentials = !expirationTime || expirationTime < new Date();
|
|
65
71
|
if (fetchNewCredentials) {
|
|
66
72
|
const fileAccessRequest = fileAccessContext.fileAccessRequest;
|
|
67
73
|
const credentials = await this.fileApi.generateProjectFileAccessToken({ projectId, fileAccessRequest });
|
|
68
|
-
credentials_mutex_so_1.credentialsCache.set(
|
|
74
|
+
credentials_mutex_so_1.credentialsCache.set(cacheKey, credentials);
|
|
69
75
|
return credentials;
|
|
70
76
|
}
|
|
71
77
|
return cachedCredentials;
|
package/package.json
CHANGED
package/src/file/file.service.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { AccessType, AWSCredentials, FileApi } from "@cirrobio/api-client";
|
|
|
2
2
|
import { ProjectFileAccessContext } from "./project-access-context";
|
|
3
3
|
import { DownloadableFile } from "./file-object.model";
|
|
4
4
|
import { credentialsCache, credentialsMutex } from "./util/credentials-mutex.so";
|
|
5
|
-
import {
|
|
5
|
+
import { GetFileUrlParams, getSignedUrl, GetSignedUrlOptions } from "./actions/sign-url.fn";
|
|
6
6
|
import { getProjectS3Bucket } from "./shared";
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -54,8 +54,9 @@ export class FileService {
|
|
|
54
54
|
* Get credentials for accessing a project file
|
|
55
55
|
*/
|
|
56
56
|
async getProjectAccessCredentials(fileAccessContext: ProjectFileAccessContext): Promise<AWSCredentials> {
|
|
57
|
+
const accessType = fileAccessContext.fileAccessRequest.accessType;
|
|
57
58
|
// Special case for project download, since we can cache the credentials
|
|
58
|
-
if (
|
|
59
|
+
if (accessType === AccessType.ProjectDownload || accessType === AccessType.SharedDatasetDownload) {
|
|
59
60
|
return this.getProjectReadCredentials(fileAccessContext);
|
|
60
61
|
}
|
|
61
62
|
return this.fileApi.generateProjectFileAccessToken({ projectId: fileAccessContext.project.id, fileAccessRequest: fileAccessContext.fileAccessRequest });
|
|
@@ -63,14 +64,20 @@ export class FileService {
|
|
|
63
64
|
|
|
64
65
|
private async getProjectReadCredentials(fileAccessContext: ProjectFileAccessContext): Promise<AWSCredentials> {
|
|
65
66
|
const projectId = fileAccessContext.project.id;
|
|
67
|
+
// Append datasetId to cache key for shared dataset downloads since we need to generate a new token for each dataset
|
|
68
|
+
let cacheKey = projectId;
|
|
69
|
+
if (fileAccessContext.fileAccessRequest.accessType === AccessType.SharedDatasetDownload) {
|
|
70
|
+
cacheKey = `${projectId}-${fileAccessContext.fileAccessRequest.datasetId}`;
|
|
71
|
+
}
|
|
72
|
+
|
|
66
73
|
return credentialsMutex.dispatch(async () => {
|
|
67
|
-
const cachedCredentials = credentialsCache.get(
|
|
74
|
+
const cachedCredentials = credentialsCache.get(cacheKey);
|
|
68
75
|
const expirationTime = cachedCredentials ? cachedCredentials?.expiration : null;
|
|
69
76
|
const fetchNewCredentials = !expirationTime || expirationTime < new Date();
|
|
70
77
|
if (fetchNewCredentials) {
|
|
71
78
|
const fileAccessRequest = fileAccessContext.fileAccessRequest;
|
|
72
79
|
const credentials = await this.fileApi.generateProjectFileAccessToken({ projectId, fileAccessRequest });
|
|
73
|
-
credentialsCache.set(
|
|
80
|
+
credentialsCache.set(cacheKey, credentials);
|
|
74
81
|
return credentials;
|
|
75
82
|
}
|
|
76
83
|
return cachedCredentials;
|