-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
218 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { IUploadProvider } from './upload.interface'; | ||
import { mkdirSync, unlink, writeFileSync } from 'fs'; | ||
import { extname } from 'path'; | ||
|
||
export class LocalStorage implements IUploadProvider { | ||
constructor(private uploadDirectory: string) {} | ||
|
||
async uploadFile(file: Express.Multer.File): Promise<any> { | ||
const now = new Date(); | ||
const year = now.getFullYear(); | ||
const month = String(now.getMonth() + 1).padStart(2, '0'); | ||
const day = String(now.getDate()).padStart(2, '0'); | ||
|
||
const dir = `${this.uploadDirectory}/${year}/${month}/${day}`; | ||
mkdirSync(dir, { recursive: true }); | ||
|
||
const randomName = Array(32) | ||
.fill(null) | ||
.map(() => Math.round(Math.random() * 16).toString(16)) | ||
.join(''); | ||
|
||
const filePath = `${dir}/${randomName}${extname(file.originalname)}`; | ||
// Logic to save the file to the filesystem goes here | ||
writeFileSync(filePath, file.buffer) | ||
|
||
return { | ||
filename: `${randomName}${extname(file.originalname)}`, | ||
path: filePath, | ||
mimetype: file.mimetype, | ||
originalname: file.originalname | ||
}; | ||
} | ||
|
||
async removeFile(filePath: string): Promise<void> { | ||
// Logic to remove the file from the filesystem goes here | ||
return new Promise((resolve, reject) => { | ||
unlink(filePath, (err) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { CloudflareStorage } from './cloudflare.storage'; | ||
import { IUploadProvider } from './upload.interface'; | ||
import { LocalStorage } from './local.storage'; | ||
|
||
export class UploadFactory { | ||
static createStorage(): IUploadProvider { | ||
const storageProvider = process.env.STORAGE_PROVIDER || 'local'; | ||
|
||
switch (storageProvider) { | ||
case 'local': | ||
return new LocalStorage(process.env.UPLOAD_DIRECTORY!); | ||
case 'cloudflare': | ||
return new CloudflareStorage( | ||
process.env.CLOUDFLARE_ACCOUNT_ID!, | ||
process.env.CLOUDFLARE_ACCESS_KEY!, | ||
process.env.CLOUDFLARE_SECRET_ACCESS_KEY!, | ||
process.env.CLOUDFLARE_REGION!, | ||
process.env.CLOUDFLARE_BUCKETNAME!, | ||
process.env.CLOUDFLARE_BUCKET_URL! | ||
); | ||
default: | ||
throw new Error(`Invalid storage type ${storageProvider}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface IUploadProvider { | ||
uploadFile(file: Express.Multer.File): Promise<any>; | ||
removeFile(filePath: string): Promise<void>; | ||
} |
Oops, something went wrong.