~mser/emote-server

c930b1a098e813764bfc2ffff7824c96b45e2905 — Michael Serajnik 8 months ago eb6e84e
Add route for deleting all frozen emotes
4 files changed, 71 insertions(+), 0 deletions(-)

M CHANGELOG.md
M README.md
M index.js
M src/util/emotes.js
M CHANGELOG.md => CHANGELOG.md +4 -0
@@ 8,6 8,10 @@ and this project adheres to

## [Unreleased]

### Added

+ Route for deleting all frozen emotes (`DELETE /frozen-emotes`)

## [1.1.0] - 2021-04-02

### Added

M README.md => README.md +23 -0
@@ 28,6 28,7 @@ API.
        + [Listing emotes](#listing-emotes)
        + [Getting emotes](#getting-emotes)
        + [Getting frozen emotes](#getting-frozen-emotes)
        + [Deleting frozen emotes](#deleting-frozen-emotes)
+ [Maintainer](#maintainer)
+ [Contribute](#contribute)
+ [License](#license)


@@ 301,6 302,28 @@ __Possible errors:__
+ `GetError`
+ `GenerationError`

###### Deleting frozen emotes

Deletes all frozen emotes (without touching their source emotes). Useful for
forcing regeneration (e.g., after overwriting an emote with another of the same
name).

__Route:__ `DELETE /frozen-emotes`

__Response on success:__

```json5
{
  "success": true,
  "message": "Delete"
}
```

__Possible errors:__

+ `AccessKeyError`
+ `DeleteError`

## Maintainer

[Michael Serajnik][maintainer]

M index.js => index.js +23 -0
@@ 214,4 214,27 @@ service.get('/frozen-emotes/:emote', async (req, res) => {
    .pipe(res)
})

service.delete('/frozen-emotes', async (req, res) => {
  if (config.accessKey !== '') {
    try {
      await schemas.emotes.validateAsync({
        accessKey: req.headers.authorization ||
          (req.query.accessKey ? `Bearer ${req.query.accessKey}` : null)
      })
    } catch {
      return res.send({
        success: false,
        error: 'AccessKeyError'
      }, 401)
    }
  }

  const deleteResponse = await emotes.deleteFrozenEmotes()

  res.send({
    success: deleteResponse.success,
    [deleteResponse.success ? 'message' : 'error']: deleteResponse.message
  }, deleteResponse.code)
})

module.exports = service

M src/util/emotes.js => src/util/emotes.js +21 -0
@@ 123,6 123,27 @@ module.exports = {
      code: 200
    }
  },
  async deleteFrozenEmotes () {
    const emotePaths = await fg(`${config.frozenEmotesPath}/*.png`)

    for (const emotePath of emotePaths) {
      try {
        await fsp.unlink(emotePath)
      } catch {
        return {
          success: false,
          message: 'DeleteError',
          code: 500
        }
      }
    }

    return {
      success: true,
      message: 'Delete',
      code: 200
    }
  },
  async getMimeType (filePath) {
    await fsp.access(filePath, fs.constants.F_OK)
    const fileType = await FileType.fromFile(filePath)