Ghost/core/server/api/canary/media.js
Naz 61b82e3ae2 Added thumbnail upload endpoint to Media API
closes https://github.com/TryGhost/Toolbox/issues/120

- Allows to update and upload brand new thumbnail images for previusly uploaded media resources
- The endpoint is available udner alpa flag as part of Admin API at `PUT /media/thumbnail/`
- As an input accepts following parameters:
- *required* `file` field containing an image file
- *required* `url` field containing parent media file URL
- *optional* `ref` as a field to put in an ID to reference the resource on the client side

- The response has following format:
```
{
  media: [{
    url: 'http://127.0.0.1:2369/content/images/1991/11/nicevideo_thumb.png'
    ref: 'unique-id-420'
  }]
}
```
2021-11-09 16:07:23 +04:00

43 lines
1.3 KiB
JavaScript

const path = require('path');
const storage = require('../../adapters/storage');
module.exports = {
docName: 'media',
upload: {
statusCode: 201,
permissions: false,
async query(frame) {
let thumbnailPath = null;
if (frame.files.thumbnail && frame.files.thumbnail[0]) {
thumbnailPath = await storage.getStorage('media').save(frame.files.thumbnail[0]);
}
const filePath = await storage.getStorage('media').save(frame.files.file[0]);
return {
filePath,
thumbnailPath
};
}
},
uploadThumbnail: {
permissions: false,
options: [
'url'
],
async query(frame) {
const mediaStorage = storage.getStorage('media');
const targetDir = path.dirname(mediaStorage.urlToPath(frame.data.url));
// NOTE: need to cleanup otherwise the parent media name won't match thumb name
// due to "unique name" generation during save
if (mediaStorage.exists(frame.file.name, targetDir)) {
await mediaStorage.delete(frame.file.name, targetDir);
}
return await mediaStorage.save(frame.file, targetDir);
}
}
};