Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 7차 세미나 실천과제 #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions Seminar7/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@

# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
yarn.lock

### Node Patch ###
# Serverless Webpack directories
.webpack/

# Optional stylelint cache

# SvelteKit build / generate output
.svelte-kit

# End of https://www.toptal.com/developers/gitignore/api/node
11 changes: 11 additions & 0 deletions Seminar7/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"watch": [
"src",
".env"
],
"ext": "js,ts,json",
"ignore": [
"src/**/*.spec.ts"
],
"exec": "ts-node --transpile-only ./src/index.ts"
}
35 changes: 35 additions & 0 deletions Seminar7/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "node-typescript-init",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon",
"build": "tsc && node dist"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/express": "^4.17.13",
"@types/jsonwebtoken": "^8.5.8",
"@types/mongoose": "^5.11.97",
"@types/multer": "^1.4.7",
"@types/multer-s3": "^2.7.12",
"@types/node": "^17.0.25",
"nodemon": "^2.0.15",
"ts-node": "^10.7.0",
"typescript": "^4.6.3"
},
"dependencies": {
"aws-sdk": "^2.1143.0",
"bcryptjs": "^2.4.3",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"express-validator": "^6.14.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^6.3.1",
"multer": "^1.4.4",
"multer-s3": "^2.10.0"
}
}
40 changes: 40 additions & 0 deletions Seminar7/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import dotenv from "dotenv";

// Set the NODE_ENV to 'development' by default
process.env.NODE_ENV = process.env.NODE_ENV || "development";

const envFound = dotenv.config();
if (envFound.error) {
// This error should crash whole process

throw new Error("⚠️ Couldn't find .env file ⚠️");
}

export default {
/**
* Your favorite port
*/
port: parseInt(process.env.PORT as string, 10) as number,

/**
* MongoDB URI
*/
mongoURI: process.env.MONGODB_URI as string,

/**
* jwt secret
*/
jwtSecret: process.env.JWT_SECRET as string,

/**
* jwt algorithm
*/
jwtAlgo: process.env.JWT_ALGO as string,

/**
* AWS S3
*/
s3AccessKey: process.env.S3_ACCESS_KEY as string,
s3SecretKey: process.env.S3_SECRET_KEY as string,
bucketName: process.env.BUCKET_NAME as string
};
19 changes: 19 additions & 0 deletions Seminar7/src/config/multer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import multer from "multer";
import multerS3 from "multer-s3";
import config from ".";
import s3 from "./s3Config";

const upload = multer({
storage: multerS3({
s3: s3,
bucket: config.bucketName,
contentType: multerS3.AUTO_CONTENT_TYPE,
acl: "public-read",
key: function (req: Express.Request, file: Express.MulterS3.File, cb) {
cb(null, `${Date.now()}_${file.originalname}`);
},
}),
});

export default upload;

10 changes: 10 additions & 0 deletions Seminar7/src/config/s3Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import AWS from "aws-sdk";
import config from ".";

const s3: AWS.S3 = new AWS.S3({
accessKeyId: config.s3AccessKey,
secretAccessKey: config.s3SecretKey,
region: 'ap-northeast-2'
});

export default s3;
51 changes: 51 additions & 0 deletions Seminar7/src/controllers/FileController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import express, { Request, Response } from "express";
import message from "../modules/responseMessage";
import statusCode from "../modules/statusCode";
import util from "../modules/util";
import { FileService } from "../services";

const uploadFileToS3 = async (req: Request, res: Response) => {
if (!req.file) return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, message.NULL_VALUE));

const image: Express.MulterS3.File = req.file as Express.MulterS3.File;
const { originalname, location } = image;

try {
const data = await FileService.createFile(location, originalname);

res.status(statusCode.CREATED).send(util.success(statusCode.CREATED, message.CREATE_FILE_SUCCESS, data));
} catch (error) {
console.log(error);
res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
}
}

const uploadFilesToS3 = async (req: Request, res: Response) => {
if (!req.files) return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, message.NULL_VALUE));

const images: Express.MulterS3.File[] = req.files as Express.MulterS3.File[];

try {
const imageList: {
location: string;
originalname: string;
}[] = await Promise.all(images.map((image: Express.MulterS3.File) => {
return {
location: image.location,
originalname: image.originalname
}
}));

const data = await FileService.createFiles(imageList);

res.status(statusCode.CREATED).send(util.success(statusCode.CREATED, message.CREATE_FILE_SUCCESS, data));
} catch (error) {
console.log(error);
res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
}
}

export default {
uploadFileToS3,
uploadFilesToS3
}
Loading