Skip to content

Commit

Permalink
fix review comments: return method and error types
Browse files Browse the repository at this point in the history
  • Loading branch information
casparneumann-cap committed Oct 24, 2023
1 parent fdc85d3 commit 7f80710
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ describe('LibraryStorage', () => {
const { addonLib } = await setup();

const addons = await storage.listAddons();
expect(addons).toContainEqual(expect.objectContaining(addonLib));
expect(addons).toEqual([addonLib]);
});

it('should count dependencies', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type ILibraryName,
type ILibraryStorage,
} from '@lumieducation/h5p-server';
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
import { ConflictException, Inject, Injectable, NotAcceptableException, NotFoundException } from '@nestjs/common';
import { S3ClientAdapter } from '@shared/infra/s3-client';
import { FileDto } from '@src/modules/files-storage/dto';
import mime from 'mime';
Expand Down Expand Up @@ -97,7 +97,7 @@ export class LibraryStorage implements ILibraryStorage {
);

if (existingLibrary !== null) {
throw new Error("Can't add library because it already exists");
throw new ConflictException("Can't add library because it already exists");
}

const library = new InstalledLibrary(libMeta, restricted, undefined);
Expand Down Expand Up @@ -406,7 +406,7 @@ export class LibraryStorage implements ILibraryStorage {

private async getMetadata(library: ILibraryName): Promise<ILibraryMetadata> {
if (!library) {
throw new Error('You must pass in a library name to getLibrary.');
throw new NotAcceptableException('You must pass in a library name to getLibrary.');
}

const result = await this.libraryRepo.findOneByNameAndVersionOrFail(
Expand All @@ -429,26 +429,28 @@ export class LibraryStorage implements ILibraryStorage {

this.checkFilename(file);

let result: { stream: Readable | never; mimetype: string; size: number | undefined } | null = null;

if (file === 'library.json') {
const metadata = await this.getMetadata(libraryName);
const stringifiedMetadata = JSON.stringify(metadata);
const readable = Readable.from(stringifiedMetadata);

return {
result = {
stream: readable,
mimetype: 'application/json',
size: stringifiedMetadata.length,
};
} else {
const response = await this.s3Client.get(this.getS3Key(libraryName, file));
const mimetype = mime.lookup(file, 'application/octet-stream');

result = {
stream: response.data,
mimetype,
size: response.contentLength,
};
}

const response = await this.s3Client.get(this.getS3Key(libraryName, file));

const mimetype = mime.lookup(file, 'application/octet-stream');

return {
stream: response.data,
mimetype,
size: response.contentLength,
};
return result;
}
}

0 comments on commit 7f80710

Please sign in to comment.