Skip to content

Commit

Permalink
fix(h5p-server): accepts library directories that look like files (#1331
Browse files Browse the repository at this point in the history
)

Fixes #1317
  • Loading branch information
sr258 authored Apr 24, 2021
1 parent a3a20ed commit 5ec660c
Show file tree
Hide file tree
Showing 9 changed files with 2,280 additions and 1,052 deletions.
3,272 changes: 2,251 additions & 1,021 deletions packages/h5p-server/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/h5p-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"crc": "^3.8.0",
"debug": "^4.1.1",
"fs-extra": "^9.0.0",
"glob-promise": "^3.4.0",
"get-all-files": "^3.0.0",
"image-size": "^1.0.0",
"jsonpath": "^1.0.2",
"merge": "^2.0.0",
Expand Down
10 changes: 6 additions & 4 deletions packages/h5p-server/src/ContentManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fsExtra from 'fs-extra';
import globPromise from 'glob-promise';
import * as path from 'path';
import { Stream, Readable } from 'stream';
import * as path from 'path';
import fsExtra from 'fs-extra';
import getAllFiles from 'get-all-files';

import { ContentMetadata } from './ContentMetadata';
import {
Expand Down Expand Up @@ -92,7 +92,9 @@ export default class ContentManager {
path.join(packageDirectory, 'content', 'content.json')
);
const otherContentFiles: string[] = (
await globPromise(path.join(packageDirectory, 'content', '**/*.*'))
await getAllFiles.async.array(
path.join(packageDirectory, 'content')
)
).filter(
(file: string) =>
path.relative(packageDirectory, file) !== 'content.json'
Expand Down
6 changes: 3 additions & 3 deletions packages/h5p-server/src/LibraryManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Readable } from 'stream';
import fsExtra from 'fs-extra';
import globPromise from 'glob-promise';
import getAllFiles from 'get-all-files';
import path from 'path';

import H5pError from './helpers/H5pError';
Expand Down Expand Up @@ -600,7 +600,7 @@ export default class LibraryManager {
}

/**
* Copies all library files from a directory (excludes library.json) to the storage.
* Copies all library file s from a directory (excludes library.json) to the storage.
* Throws errors if something went wrong.
* @param fromDirectory The directory to copy from
* @param libraryInfo the library object
Expand All @@ -611,7 +611,7 @@ export default class LibraryManager {
libraryInfo: ILibraryName
): Promise<void> {
log.info(`copying library files from ${fromDirectory}`);
const files: string[] = await globPromise(`${fromDirectory}/**/*.*`);
const files: string[] = await getAllFiles.async.array(fromDirectory);
await Promise.all(
files.map((fileFullPath: string) => {
const fileLocalPath: string = path.relative(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ReadStream } from 'fs';
import fsExtra from 'fs-extra';
import getAllFiles from 'get-all-files';
import path from 'path';
import promisepipe from 'promisepipe';
import globPromise from 'glob-promise';

import {
ITemporaryFile,
Expand Down Expand Up @@ -127,11 +127,8 @@ export default class DirectoryTemporaryFileStorage
return (
await Promise.all(
users.map(async (u) => {
const filesOfUser = await globPromise(
path.join(
this.getAbsoluteUserDirectoryPath(u),
'**/*.*'
)
const filesOfUser = await getAllFiles.async.array(
this.getAbsoluteUserDirectoryPath(u)
);
return Promise.all(
filesOfUser
Expand Down
21 changes: 9 additions & 12 deletions packages/h5p-server/src/implementation/fs/FileContentStorage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ReadStream } from 'fs';
import { Stream } from 'stream';
import fsExtra from 'fs-extra';
import globPromise from 'glob-promise';
import getAllFiles from 'get-all-files';
import path from 'path';
import promisepipe from 'promisepipe';
import { Stream } from 'stream';

import { streamToString } from '../../helpers/StreamHelpers';
import {
Expand Down Expand Up @@ -438,17 +438,14 @@ export default class FileContentStorage implements IContentStorage {
this.getContentPath(),
contentId.toString()
);
const absolutePaths = await globPromise(
path.join(contentDirectoryPath, '**', '*.*'),
{
ignore: [
path.join(contentDirectoryPath, 'content.json'),
path.join(contentDirectoryPath, 'h5p.json')
],
nodir: true
}
const absolutePaths = await getAllFiles.async.array(
path.join(contentDirectoryPath)
);
return absolutePaths.map((p) => path.relative(contentDirectoryPath, p));
const contentPath = path.join(contentDirectoryPath, 'content.json');
const h5pPath = path.join(contentDirectoryPath, 'h5p.json');
return absolutePaths
.filter((p) => p !== contentPath && p !== h5pPath)
.map((p) => path.relative(contentDirectoryPath, p));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Readable } from 'stream';
import fsExtra from 'fs-extra';
import globPromise from 'glob-promise';
import getAllFiles from 'get-all-files';
import path from 'path';
import promisepipe from 'promisepipe';
import { Readable } from 'stream';
import upath from 'upath';

import { checkFilename } from './filenameUtils';
Expand Down Expand Up @@ -430,10 +430,11 @@ export default class FileLibraryStorage implements ILibraryStorage {
*/
public async listFiles(library: ILibraryName): Promise<string[]> {
const libPath = this.getDirectoryPath(library);
return (await globPromise(path.join(libPath, '**/*.*')))
return (await getAllFiles.async.array(libPath))
.map((p) => path.relative(libPath, p))
.filter((p) => !this.isIgnored(p))
.map((p) => upath.toUnix(p));
.map((p) => upath.toUnix(p))
.sort();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/h5p-server/test/FileContentStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('FileContentStorage (repository that saves content objects to a local d
stream2.push(null);
await storage.addFile(id, 'file2.txt', stream2, user);
const files = await storage.listFiles(id, user);
expect(files).toMatchObject(['file1.txt', 'file2.txt']);
expect(files.sort()).toMatchObject(['file1.txt', 'file2.txt']);
},
{ keep: false, unsafeCleanup: true }
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ describe('CachedLibraryStorage', () => {
[
'greetingcard.css',
'greetingcard.js',
'language/.en.json',
'language/de.json',
'library.json',
'semantics.json'
Expand Down

0 comments on commit 5ec660c

Please sign in to comment.