Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #68 from rmagur1203/develop
Browse files Browse the repository at this point in the history
edit: upload rename to copyFile & unlink
  • Loading branch information
rmagur1203 authored Jun 10, 2022
2 parents 17ae52c + c3cb7c0 commit cbe7743
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/services/upload.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Inject, Injectable } from '@decorators/di';
import { UploadBody } from '../types/upload';
import { AttachmentEntity } from '../entities/Attachment';
import { readFile, rename } from 'fs';
import { copyFile, readFile, unlink } from 'fs';
import path from 'path';
import { MD5, SHA1 } from '../modules/hash';
import { Repository } from 'typeorm';
import { UserEntity } from '../entities/User';
import { IUser } from '../types/user';
import logger from '../modules/logger';

@Injectable()
export class UploadService {
Expand Down Expand Up @@ -51,10 +52,11 @@ export class UploadService {
return new Promise((resolve, reject) =>
readFile(path.resolve(process.cwd(), file.path), (err, data) => {
if (err) return reject(err);
rename(
copyFile(
file.path,
path.resolve(UPLOAD_PATH, SHA1(data)),
async (err) => {
unlink(file.path, (err) => err && logger.error(err));
if (err) return reject(err);
let prev = await this.attachmentRepository.findOne({
where: {
Expand Down

0 comments on commit cbe7743

Please sign in to comment.