feat: copy page to different space (#1118)

* Add copy page to space endpoint
* copy storage function
* copy function
* feat: copy attachments too
* Copy page - WIP
* fix type
* sync
* cleanup
This commit is contained in:
Philip Okugbe
2025-04-30 14:43:16 +01:00
committed by GitHub
parent 0402f7efb5
commit de7982fe30
17 changed files with 441 additions and 14 deletions

View File

@ -25,6 +25,16 @@ export class LocalDriver implements StorageDriver {
}
}
async copy(fromFilePath: string, toFilePath: string): Promise<void> {
try {
if (await this.exists(fromFilePath)) {
await fs.copy(fromFilePath, toFilePath);
}
} catch (err) {
throw new Error(`Failed to copy file: ${(err as Error).message}`);
}
}
async read(filePath: string): Promise<Buffer> {
try {
return await fs.readFile(this._fullPath(filePath));

View File

@ -1,5 +1,6 @@
import { S3StorageConfig, StorageDriver, StorageOption } from '../interfaces';
import {
CopyObjectCommand,
DeleteObjectCommand,
GetObjectCommand,
HeadObjectCommand,
@ -39,6 +40,22 @@ export class S3Driver implements StorageDriver {
}
}
async copy(fromFilePath: string, toFilePath: string): Promise<void> {
try {
if (await this.exists(fromFilePath)) {
await this.s3Client.send(
new CopyObjectCommand({
Bucket: this.config.bucket,
CopySource: `${this.config.bucket}/${fromFilePath}`,
Key: toFilePath,
}),
);
}
} catch (err) {
throw new Error(`Failed to copy file: ${(err as Error).message}`);
}
}
async read(filePath: string): Promise<Buffer> {
try {
const command = new GetObjectCommand({

View File

@ -1,6 +1,8 @@
export interface StorageDriver {
upload(filePath: string, file: Buffer): Promise<void>;
copy(fromFilePath: string, toFilePath: string): Promise<void>;
read(filePath: string): Promise<Buffer>;
exists(filePath: string): Promise<boolean>;

View File

@ -14,6 +14,11 @@ export class StorageService {
this.logger.debug(`File uploaded successfully. Path: ${filePath}`);
}
async copy(fromFilePath: string, toFilePath: string) {
await this.storageDriver.copy(fromFilePath, toFilePath);
this.logger.debug(`File copied successfully. Path: ${toFilePath}`);
}
async read(filePath: string): Promise<Buffer> {
return this.storageDriver.read(filePath);
}