diff --git a/.gitignore b/.gitignore index 0cf5790..4630602 100644 --- a/.gitignore +++ b/.gitignore @@ -56,5 +56,5 @@ pids report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json # File upload -uploads/* -!uploads/.gitkeep +static/uploads/* +!static/uploads/.gitkeep diff --git a/docs/s3-api-docs.md b/docs/s3-api-docs.md index f90974d..575fcc2 100644 --- a/docs/s3-api-docs.md +++ b/docs/s3-api-docs.md @@ -66,7 +66,7 @@ Example Response "id": 1, "createdAt": "2024-05-06T20:53:30.271Z", "updatedAt": "2024-05-06T20:53:30.272Z", - "imageUrl": "/uploads/dd229dab-84b2-4000-879e-a38dbb1c8796.jpeg", + "imageUrl": "/static/uploads/dd229dab-84b2-4000-879e-a38dbb1c8796.jpeg", "caption": "This is my first post!", "author": 1, "stickers": [ @@ -143,7 +143,7 @@ Request Body ## Posts -The API provides endpoints to retrieve all posts, like a post, unlike a post, comment on a post, and create a new post. +The API provides endpoints to retrieve all posts, like a post, unlike a post, comment on a post, create a new post, and get available stickers. ### Get All Posts @@ -162,7 +162,7 @@ Example Response "id": 1, "createdAt": "2024-05-06T20:53:30.271Z", "updatedAt": "2024-05-06T20:53:30.272Z", - "imageUrl": "/uploads/dd229dab-84b2-4000-879e-a38dbb1c8796.jpeg", + "imageUrl": "/static/uploads/dd229dab-84b2-4000-879e-a38dbb1c8796.jpeg", "caption": "This is my first post!", "author": { "id": 1, @@ -222,7 +222,7 @@ Example Response "id": 2, "createdAt": "2024-05-06T20:54:39.213Z", "updatedAt": "2024-05-06T20:54:39.213Z", - "imageUrl": "/uploads/3037f4d9-a335-434c-b5c3-62cf1d181c64.jpeg", + "imageUrl": "/static/uploads/3037f4d9-a335-434c-b5c3-62cf1d181c64.jpeg", "caption": "This is my second post!", "author": { "id": 1, @@ -317,3 +317,29 @@ Request Body ``` + +### Get Available Stickers + +
+ View details + +``` +GET /posts/stickers +``` + +Example Response + +```json +[ + { + "name": "a", + "imageUrl": "/static/stickers/a.png" + }, + { + "name": "b", + "imageUrl": "/static/stickers/b.png" + } +] +``` + +
diff --git a/src/app.module.ts b/src/app.module.ts index 08769e5..156bf05 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -28,8 +28,8 @@ import { UsersModule } from './users/users.module'; }), PostsModule, ServeStaticModule.forRoot({ - rootPath: join(__dirname, '..', 'uploads'), - serveRoot: '/uploads', + rootPath: join(__dirname, '..', 'static'), + serveRoot: '/static', }), UsersModule, ], diff --git a/src/posts/constants.ts b/src/posts/constants.ts new file mode 100644 index 0000000..c30ed79 --- /dev/null +++ b/src/posts/constants.ts @@ -0,0 +1,6 @@ +export const stickers = [ + { + name: 'jonas', + imageUrl: '/static/stickers/jonas.png', + }, +]; diff --git a/src/posts/posts.controller.ts b/src/posts/posts.controller.ts index 1402d15..9ccf0c4 100644 --- a/src/posts/posts.controller.ts +++ b/src/posts/posts.controller.ts @@ -15,6 +15,7 @@ import { randomUUID } from 'crypto'; import { diskStorage } from 'multer'; import { extname } from 'path'; import { User, UserPayload } from 'src/auth/decorators/user.decorator'; +import { stickers } from './constants'; import { CommentPostDto } from './dto/comment-post.dto'; import { CreatePostDto } from './dto/create-post.dto'; import { PostsService } from './posts.service'; @@ -27,7 +28,7 @@ export class PostsController { @UseInterceptors( FileInterceptor('image', { storage: diskStorage({ - destination: './uploads', + destination: './static/uploads', filename: (_, file, cb) => { const filename = randomUUID() + extname(file.originalname); cb(null, filename); @@ -71,4 +72,9 @@ export class PostsController { ) { return this.postsService.comment(+id, user.sub, commentPostDto); } + + @Get('stickers') + getStickers() { + return stickers; + } } diff --git a/src/posts/posts.service.ts b/src/posts/posts.service.ts index 4cc6c09..e4d0556 100644 --- a/src/posts/posts.service.ts +++ b/src/posts/posts.service.ts @@ -28,7 +28,7 @@ export class PostsService { ) { const post = this.postRepository.create({ ...createPostDto, - imageUrl: file ? `/uploads/${file.filename}` : undefined, + imageUrl: file ? `/static/uploads/${file.filename}` : undefined, author: user.sub, }); await this.em.flush(); diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 722c1c5..1aa5f37 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -31,7 +31,7 @@ export class UsersController { @UseInterceptors( FileInterceptor('image', { storage: diskStorage({ - destination: './uploads', + destination: './static/uploads', filename: (_, file, cb) => { const filename = randomUUID() + extname(file.originalname); cb(null, filename); diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 5873da0..55be1d1 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -45,7 +45,7 @@ export class UsersService { const user = await this.userRepository.findOneOrFail({ id }); // @ts-expect-error wrap causes error in type system wrap(user).assign(updateUserDto); - if (file) user.imageUrl = `/uploads/${file.filename}`; + if (file) user.imageUrl = `/static/uploads/${file.filename}`; await this.em.flush(); return user; } diff --git a/static/stickers/jonas.png b/static/stickers/jonas.png new file mode 100644 index 0000000..11b4baf Binary files /dev/null and b/static/stickers/jonas.png differ diff --git a/uploads/.gitkeep b/static/uploads/.gitkeep similarity index 100% rename from uploads/.gitkeep rename to static/uploads/.gitkeep