# Image File upload

### File upload module path

<pre><code>├── node_modules
<strong>├── mail-templates
</strong>├── migrations
├── src
│   ├── configs
│   ├── domains
│   │   ├── file-uploads      # File uploads module
</code></pre>

### Upload an image

Find out the root below in the controller  <mark style="background-color:blue;">src/domains/file-uploads/file-uploads.controller.ts</mark>

```typescript
  @Post('/image')
  @ApiOperation({
    operationId: 'Upload an image',
    description: 'Upload a file containing an image',
  })
  @UseInterceptors(
    FileInterceptor('file', {
      fileFilter: (req, file, cb) => {
        if (file.size > 1000 * 1000) {
          cb(new BadRequestException('Image is too large'), false);
        } else if (!file.mimetype.startsWith('image')) {
          cb(new BadRequestException('Invalid file format'), false);
        } else {
          cb(null, true);
        }
      },
    }),
  )
  async uploadImage(@UploadedFile() file): Promise<FileUpload> {
    return await this.service.saveFile(file, 'image');
  }
```

### Read an image

Find out the root below in the controller  <mark style="background-color:blue;">src/domains/file-uploads/file-uploads.controller.ts</mark>

```typescript
  @Get('/:id/image')
  @ApiOperation({
    operationId: 'Get an image',
  })
  async getImage(@Res() res, @Param('id') id: number) {
    const image = await this.service.repo.findOne({ where: { id, type: 'image' }});
    if (!!image && existsSync(image.path)) {
      res.set('Content-Type', image.mimetype);
      createReadStream(image.path).pipe(res);
    } else {
      throw new BadRequestException(`File with id ${id} does not exists`);
    }
  }
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.nzoni.app/nest.js/image-file-upload.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
