Skip to content

Commit

Permalink
fix(interactive video): implemented getFileStat for S3
Browse files Browse the repository at this point in the history
  • Loading branch information
sr258 committed Jul 4, 2020
1 parent 94e2b1e commit 9779cbd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
37 changes: 34 additions & 3 deletions src/implementation/db/MongoS3ContentStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,43 @@ export default class MongoS3ContentStorage implements IContentStorage {
* @param user the user who wants to retrieve the content file
* @returns
*/
getFileStats(
public async getFileStats(
contentId: string,
file: string,
filename: string,
user: IUser
): Promise<IFileStats> {
return null;
validateFilename(filename);

if (
!(await this.getUserPermissions(contentId, user)).includes(
Permission.View
)
) {
log.error(
`User tried to get stats of file from a content object without proper permissions.`
);
throw new H5pError(
'mongo-s3-content-storage:missing-view-permission',
{},
403
);
}

try {
const head = await this.s3
.headObject({
Bucket: this.options.s3Bucket,
Key: MongoS3ContentStorage.getS3Key(contentId, filename)
})
.promise();
return { size: head.ContentLength, birthtime: head.LastModified };
} catch (error) {
throw new H5pError(
'content-file-missing',
{ filename, contentId },
404
);
}
}

/**
Expand Down
29 changes: 28 additions & 1 deletion src/implementation/db/S3TemporaryFileStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,34 @@ export default class S3TemporaryFileStorage implements ITemporaryFileStorage {
filename: string,
user: IUser
): Promise<IFileStats> {
return undefined;
validateFilename(filename);

if (
!(await this.getUserPermissions(user.id, filename)).includes(
Permission.View
)
) {
log.error(
`User tried to get stats of a content object without proper permissions.`
);
throw new H5pError(
's3-temporary-storage:missing-view-permission',
{},
403
);
}

try {
const head = await this.s3
.headObject({
Bucket: this.options.s3Bucket,
Key: filename
})
.promise();
return { size: head.ContentLength, birthtime: head.LastModified };
} catch (error) {
throw new H5pError('file-not-found', {}, 404);
}
}

/**
Expand Down

0 comments on commit 9779cbd

Please sign in to comment.