2019-04-07 14:50:36 +02:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
|
|
|
import { DriveFolders, DriveFiles } from '..';
|
|
|
|
import { DriveFolder } from '../entities/drive-folder';
|
2019-04-12 18:43:22 +02:00
|
|
|
import { ensure } from '../../prelude/ensure';
|
2019-04-23 15:35:26 +02:00
|
|
|
import { awaitAll } from '../../prelude/await-all';
|
2019-06-27 11:04:09 +02:00
|
|
|
import { SchemaType } from '../../misc/schema';
|
2019-04-23 15:35:26 +02:00
|
|
|
|
|
|
|
export type PackedDriveFolder = SchemaType<typeof packedDriveFolderSchema>;
|
2019-04-07 14:50:36 +02:00
|
|
|
|
|
|
|
@EntityRepository(DriveFolder)
|
|
|
|
export class DriveFolderRepository extends Repository<DriveFolder> {
|
|
|
|
public validateFolderName(name: string): boolean {
|
|
|
|
return (
|
|
|
|
(name.trim().length > 0) &&
|
|
|
|
(name.length <= 200)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async pack(
|
|
|
|
src: DriveFolder['id'] | DriveFolder,
|
|
|
|
options?: {
|
|
|
|
detail: boolean
|
|
|
|
}
|
2019-04-23 15:35:26 +02:00
|
|
|
): Promise<PackedDriveFolder> {
|
2019-04-07 14:50:36 +02:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false
|
|
|
|
}, options);
|
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
const folder = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2019-04-23 15:35:26 +02:00
|
|
|
return await awaitAll({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: folder.id,
|
2019-04-23 15:35:26 +02:00
|
|
|
createdAt: folder.createdAt.toISOString(),
|
2019-04-07 14:50:36 +02:00
|
|
|
name: folder.name,
|
|
|
|
parentId: folder.parentId,
|
|
|
|
|
|
|
|
...(opts.detail ? {
|
|
|
|
foldersCount: DriveFolders.count({
|
|
|
|
parentId: folder.id
|
|
|
|
}),
|
|
|
|
filesCount: DriveFiles.count({
|
|
|
|
folderId: folder.id
|
|
|
|
}),
|
|
|
|
|
|
|
|
...(folder.parentId ? {
|
|
|
|
parent: this.pack(folder.parentId, {
|
|
|
|
detail: true
|
|
|
|
})
|
|
|
|
} : {})
|
|
|
|
} : {})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 15:35:26 +02:00
|
|
|
|
|
|
|
export const packedDriveFolderSchema = {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this Drive folder.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
format: 'date-time',
|
|
|
|
description: 'The date that the Drive folder was created.'
|
|
|
|
},
|
|
|
|
name: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
description: 'The folder name.',
|
|
|
|
},
|
|
|
|
foldersCount: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'number' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
description: 'The count of child folders.',
|
|
|
|
},
|
|
|
|
filesCount: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'number' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
description: 'The count of child files.',
|
|
|
|
},
|
|
|
|
parentId: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
format: 'id',
|
|
|
|
description: 'The parent folder ID of this folder.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
parent: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
ref: 'DriveFolder'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|