Skip to content

Commit

Permalink
Add: 파일 이동 api
Browse files Browse the repository at this point in the history
  • Loading branch information
leehj050211 committed Mar 29, 2022
1 parent 8b563a1 commit 9b36a38
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/drive/drive.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ export class DriveController {
return this.driveService.deleteFile(user.memberCode, FileDto);
}

@Post('move/:driveId/:folderId/:fileId')
moveFile(
@GetUser() user: User,
@Param() FileDto,
@Body('newFolderId') newFolderId: string
) {
return this.driveService.moveFile(user.memberCode, FileDto, newFolderId);
}

@Post('share/:driveId/:folderId/:fileId')
shareFile(
@GetUser() user: User,
Expand Down
64 changes: 62 additions & 2 deletions src/drive/drive.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,66 @@ export class DriveService {
return;
}

async moveFile(usercode: number, fileDto: FileDto, newFolderId: string) {
const {driveId: inputDriveId, folderId: oldFolderId} = fileDto;
if (oldFolderId === newFolderId) {
throw new ConflictException('Same directory');
}
// driveId check
const drive = await this.driveRepository.getDriveByUsercode(usercode);
if (!drive) {
throw new NotFoundException('Drive not found');
}
const driveId = drive.id.toString('hex');
if (inputDriveId !== driveId) {
throw new BadRequestException(`Drive doesn't match`);
}

// file check
const file = await this.fileRepository.getFileByFileDto(fileDto);
if (!file) {
throw new NotFoundException('File not found');
}
const fileName = file.fileName.toString('hex');

let oldDirInfo: {folderId: Buffer, folderName: string}[] = [];
let newDirInfo: {folderId: Buffer, folderName: string}[] = [];
let oldDir = '';
let newDir = '';

// folder check
if (oldFolderId !== 'root') {
oldDirInfo = await this.folderRepository.getDir({driveId, folderId: oldFolderId});
if (!oldDirInfo.length) {
throw new NotFoundException('Folder not found');
}
oldDir = oldDirInfo.map(e => {
return e.folderId.toString('hex');
}).join('/');
}
if (newFolderId !== 'root') {
newDirInfo = await this.folderRepository.getDir({driveId, folderId: newFolderId});
if (!newDirInfo.length) {
throw new NotFoundException('Folder not found');
}
newDir = newDirInfo.map(e => {
return e.folderId.toString('hex');
}).join('/');
}

const oldFilePath = `${storagePath}/${driveId}/${oldFolderId === 'root'? '': oldDir+'/'}${fileName}`;
const newFilePath = `${storagePath}/${driveId}/${newFolderId === 'root'? '': newDir+'/'}${fileName}`;
try {
await Promise.all([
await this.fileRepository.moveFile(fileDto, newFolderId),
await fs.promises.rename(oldFilePath, newFilePath)
]);
} catch(error) {
console.error(error);
throw new InternalServerErrorException('Failed to move folder');
}
}

async shareFile(usercode: number, fileDto: FileDto, share: boolean) {
const {driveId: inputDriveId} = fileDto;
// driveId check
Expand Down Expand Up @@ -455,7 +515,7 @@ export class DriveService {
this.folderRepository.getDir(folderDto),
this.folderRepository.getDir({driveId, folderId: newFolderId})
]);
if (!newDirInfo) {
if (!newDirInfo.length) {
throw new NotFoundException('Folder not found');
}
newDir = newDirInfo.map(e => {
Expand All @@ -464,7 +524,7 @@ export class DriveService {
} else {
oldDirInfo = await this.folderRepository.getDir(folderDto);
}
if (!oldDirInfo) {
if (!oldDirInfo.length) {
throw new NotFoundException('Folder not found');
}
oldDir = oldDirInfo.map(e => {
Expand Down
18 changes: 18 additions & 0 deletions src/drive/repository/file.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,22 @@ export class FileRepository extends Repository<File> {
throw new InternalServerErrorException();
}
}

async moveFile(
fileDto: FileDto,
newFolderId: string
):Promise<void> {
const {driveId, folderId} = fileDto;
try {
this.update({
driveId: new Buffer(driveId, 'hex'),
folderId: new Buffer(folderId, 'hex')
}, {
folderId: newFolderId === 'root'? null: new Buffer(newFolderId, 'hex')
})
} catch (error) {
console.error(error);
throw new InternalServerErrorException();
}
}
}
2 changes: 1 addition & 1 deletion views/pages/home.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<main>
<div>
<h1 class="main">BSM Cloud</h1>
<p class="main">아주 환상적인 클라우드 서비스</p>
<p class="main">아주 환상적인 파일 공유 서비스</p>
<form id="share_code_box" onsubmit="loadFile($('#share_code').value);return false;">
<input class="input_text" type="text" id="share_code" required placeholder="간편 공유 코드 4자리 입력" maxlength="4">
<button class="button" type="submit">입력</button>
Expand Down

0 comments on commit 9b36a38

Please sign in to comment.