fix(SPG-611) Upload - pemindahan directory upload image

pull/36/head
Aswin Ashar Abdullah 2024-07-22 18:33:50 +07:00
parent 1ff5461311
commit 679b98d198
9 changed files with 67 additions and 40 deletions

View File

@ -45,6 +45,7 @@
"dotenv": "^16.4.5",
"elastic-apm-node": "^4.5.4",
"exceljs": "^4.4.0",
"fs-extra": "^11.2.0",
"googleapis": "^140.0.0",
"handlebars": "^4.7.8",
"mathjs": "^13.0.2",

View File

@ -1,39 +1,28 @@
import * as fs from 'fs';
import * as fs from 'fs-extra';
import * as path from 'path';
export function MoveFilePathHelper() {}
export async function MoveFilePathHelper(data) {
const imagePath = data['qr_image'] ?? data['image_url'];
const sourcePath = path.join(__dirname, '../../../../uploads/', imagePath);
const movePath =
'data/' +
imagePath
.split('/')
.filter((item) => !['uploads', 'tmp'].includes(item))
.join('/');
const destinationPath = path.join(
__dirname,
'../../../../uploads/',
movePath,
);
// export class MoveFileToDirIdHelper {
// public srcDir: string;
// public fileName: string;
try {
await fs.move(sourcePath, destinationPath);
// constructor(private file_url: string = null) {}
// execute(): string {
// try {
// this.getSrcDir();
// this.getFileName();
// const copyFile = `${this.fileName}-copy`;
// fs.mkdirSync(`./uploads/${this.srcDir}`, { recursive: true });
// fs.copyFileSync(
// this.file_url,
// `./uploads/${this.srcDir}/${this.fileName}`,
// );
// fs.unlinkSync(`${this.file_url}`);
// this.file_url = `uploads/${this.srcDir}/${this.fileName}`;
// } catch (error) {
// console.error('Error moving file:', error);
// }
// return this.file_url;
// }
// private getSrcDir(): void {
// this.srcDir = this.file_url.split('/').slice(2, -1).join('/');
// }
// private getFileName(): void {
// this.fileName = this.file_url.split('/').slice(-1).join('/');
// }
// }
Object.assign(data, {
image_url: movePath,
});
} catch (error) {
console.log(`Failed! Error move file data`);
}
}

View File

@ -6,6 +6,7 @@ import {
columnUniques,
validateRelations,
} from 'src/core/strings/constants/interface.constants';
import { MoveFilePathHelper } from 'src/core/helpers/path/move-file-path.helper';
export abstract class BaseCreateManager<Entity> extends BaseManager {
protected result: Entity;
@ -43,6 +44,15 @@ export abstract class BaseCreateManager<Entity> extends BaseManager {
}
async process(): Promise<void> {
const keys = Object.keys(this.data);
if (
(keys.includes('qr_image') || keys.includes('image_url')) &&
(this.data['image_url']?.includes('tmp') ||
this.data['qr_image']?.includes('tmp'))
) {
await MoveFilePathHelper(this.data);
}
this.result = await this.dataService.create(
this.queryRunner,
this.entityTarget,

View File

@ -0,0 +1,17 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class UpdateImageColumnItem1721647955446 implements MigrationInterface {
name = 'UpdateImageColumnItem1721647955446';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "items" RENAME COLUMN "image" TO "image_url"`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "items" RENAME COLUMN "image_url" TO "image"`,
);
}
}

View File

@ -25,8 +25,8 @@ export class ItemModel
@Column('varchar', { name: 'name' })
name: string;
@Column('varchar', { name: 'image', nullable: true })
image: string;
@Column('varchar', { name: 'image_url', nullable: true })
image_url: string;
@Column('enum', {
name: 'item_type',

View File

@ -5,7 +5,7 @@ import { LimitType } from '../../constants';
export interface ItemEntity extends BaseStatusEntity {
name: string;
item_type: ItemType;
image: string;
image_url: string;
hpp: number;
sales_margin: number;

View File

@ -33,6 +33,7 @@ export class DetailItemManager extends BaseDetailManager<ItemEntity> {
get selects(): string[] {
return [
`${this.tableName}.id`,
`${this.tableName}.image_url`,
`${this.tableName}.created_at`,
`${this.tableName}.status`,
`${this.tableName}.item_type`,

View File

@ -29,7 +29,7 @@ export class ItemDto extends BaseStatusDto implements ItemEntity {
})
@IsString()
@ValidateIf((body) => body.image)
image: string;
image_url: string;
@ApiProperty({
type: 'string',

View File

@ -3423,6 +3423,15 @@ fs-extra@^10.0.0:
jsonfile "^6.0.1"
universalify "^2.0.0"
fs-extra@^11.2.0:
version "11.2.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-11.2.0.tgz#e70e17dfad64232287d01929399e0ea7c86b0e5b"
integrity sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw==
dependencies:
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^2.0.0"
fs-minipass@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-2.1.0.tgz#7f5036fdbf12c63c169190cbe4199c852271f9fb"