Skip to content

Commit

Permalink
feat: add stickers, move uploads dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Frost committed May 19, 2024
1 parent c424681 commit ad5d5d5
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 30 additions & 4 deletions docs/s3-api-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -317,3 +317,29 @@ Request Body
```

</details>

### Get Available Stickers

<details>
<summary>View details</summary>

```
GET /posts/stickers
```

Example Response

```json
[
{
"name": "a",
"imageUrl": "/static/stickers/a.png"
},
{
"name": "b",
"imageUrl": "/static/stickers/b.png"
}
]
```

</details>
4 changes: 2 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand Down
6 changes: 6 additions & 0 deletions src/posts/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const stickers = [
{
name: 'jonas',
imageUrl: '/static/stickers/jonas.png',
},
];
8 changes: 7 additions & 1 deletion src/posts/posts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -71,4 +72,9 @@ export class PostsController {
) {
return this.postsService.comment(+id, user.sub, commentPostDto);
}

@Get('stickers')
getStickers() {
return stickers;
}
}
2 changes: 1 addition & 1 deletion src/posts/posts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Binary file added static/stickers/jonas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.

0 comments on commit ad5d5d5

Please sign in to comment.