~mser/emote-server

99061d9f84ea1d08cb890b120028607fa27ed375 — Michael Serajnik 4 months ago d64d1f5
Add optional file size limit to the HTTP API
7 files changed, 21 insertions(+), 0 deletions(-)

M .env.docker
M .env.example
M CHANGELOG.md
M README.md
M docker-compose.yml.example
M src/config/index.js
M src/util/emotes.js
M .env.docker => .env.docker +1 -0
@@ 6,5 6,6 @@ EMOTE_SERVER_NUMBER_OF_WORKERS=$EMOTE_SERVER_NUMBER_OF_WORKERS

# Files
EMOTE_SERVER_SUPPORTED_FILE_EXTENSIONS=$EMOTE_SERVER_SUPPORTED_FILE_EXTENSIONS
EMOTE_SERVER_FILE_SIZE_LIMIT=$EMOTE_SERVER_FILE_SIZE_LIMIT
EMOTE_SERVER_EMOTES_PATH=$EMOTE_SERVER_EMOTES_PATH
EMOTE_SERVER_FROZEN_EMOTES_PATH=$EMOTE_SERVER_FROZEN_EMOTES_PATH

M .env.example => .env.example +1 -0
@@ 6,5 6,6 @@ EMOTE_SERVER_NUMBER_OF_WORKERS=

# Files
EMOTE_SERVER_SUPPORTED_FILE_EXTENSIONS=png,gif,apng
EMOTE_SERVER_FILE_SIZE_LIMIT=0
EMOTE_SERVER_EMOTES_PATH=./emotes
EMOTE_SERVER_FROZEN_EMOTES_PATH=./frozen-emotes

M CHANGELOG.md => CHANGELOG.md +4 -0
@@ 8,6 8,10 @@ and this project adheres to

## [Unreleased]

### Added

+ Optional file size limit when uploading via HTTP API

## [1.2.0] - 2021-04-02

### Added

M README.md => README.md +3 -0
@@ 148,6 148,8 @@ attention to the instructions to prevent issues.
+ `EMOTE_SERVER_SUPPORTED_FILE_EXTENSIONS=png,gif,apng`: sets the file
  extensions for the files the server should serve. The extensions need to be
  separated with `,`.
+ `EMOTE_SERVER_FILE_SIZE_LIMIT=0`: sets the file size limit in bytes when
  uploading files via the HTTP API. If set to `0`, there is no limit.
+ `EMOTE_SERVER_EMOTES_PATH=./emotes`: the path emotes are served from. Can be
  relative or absolute.



@@ 225,6 227,7 @@ __Response on success:__
__Possible errors:__

+ `AccessKeyError`
+ `FileSizeError`
+ `AddError`

###### Deleting emotes

M docker-compose.yml.example => docker-compose.yml.example +1 -0
@@ 17,5 17,6 @@ services:
      - EMOTE_SERVER_ACCESS_KEY=
      - EMOTE_SERVER_NUMBER_OF_WORKERS=
      - EMOTE_SERVER_SUPPORTED_FILE_EXTENSIONS=png,gif,apng
      - EMOTE_SERVER_FILE_SIZE_LIMIT=0
      - EMOTE_SERVER_EMOTES_PATH=/data/emotes
      - EMOTE_SERVER_FROZEN_EMOTES_PATH=/data/frozen-emotes

M src/config/index.js => src/config/index.js +1 -0
@@ 22,6 22,7 @@ module.exports = {
  accessKey: process.env.EMOTE_SERVER_ACCESS_KEY,
  supportedFileExtensions:
    process.env.EMOTE_SERVER_SUPPORTED_FILE_EXTENSIONS || 'png,gif',
  fileSizeLimit: process.env.EMOTE_SERVER_FILE_SIZE_LIMIT || 0,
  emotesPath: emotesPath || path.resolve(__dirname, '../..', './emotes'),
  frozenEmotesPath:
    frozenEmotesPath || path.resolve(__dirname, '../..', './frozen-emotes')

M src/util/emotes.js => src/util/emotes.js +10 -0
@@ 54,6 54,16 @@ module.exports = {
      }
    }

    if (config.fileSizeLimit > 0) {
      if (Buffer.byteLength(file.data) > config.fileSizeLimit) {
        return {
          success: false,
          message: 'FileSizeError',
          code: 400
        }
      }
    }

    try {
      await fsp.writeFile(`${config.emotesPath}/${file.name}`, file.data)
    } catch {