mirror of https://github.com/laurent22/joplin.git
Plugins: Allow updating a resource via the data API
parent
6458ad0540
commit
74273cd570
|
@ -379,6 +379,28 @@ describe('services_rest_Api', function() {
|
|||
expect(resourceV2.size).toBe((await shim.fsDriver().stat(Resource.fullPath(resourceV2))).size);
|
||||
}));
|
||||
|
||||
it('should allow updating a resource file only', (async () => {
|
||||
await api.route(RequestMethod.POST, 'resources', null, JSON.stringify({
|
||||
title: 'resource',
|
||||
}), [{ path: `${supportDir}/photo.jpg` }]);
|
||||
|
||||
const resourceV1: ResourceEntity = (await Resource.all())[0];
|
||||
|
||||
await msleep(1);
|
||||
|
||||
await api.route(RequestMethod.PUT, `resources/${resourceV1.id}`, null, null, [
|
||||
{
|
||||
path: `${supportDir}/photo-large.png`,
|
||||
},
|
||||
]);
|
||||
|
||||
const resourceV2: ResourceEntity = (await Resource.all())[0];
|
||||
|
||||
// It should have updated the file content, but not the metadata
|
||||
expect(resourceV2.title).toBe(resourceV1.title);
|
||||
expect(resourceV2.size).toBeGreaterThan(resourceV1.size);
|
||||
}));
|
||||
|
||||
it('should update resource properties', (async () => {
|
||||
await api.route(RequestMethod.POST, 'resources', null, JSON.stringify({
|
||||
title: 'resource',
|
||||
|
|
|
@ -235,6 +235,8 @@ function shimInit(options = null) {
|
|||
const readChunk = require('read-chunk');
|
||||
const imageType = require('image-type');
|
||||
|
||||
const isUpdate = !!options.destinationResourceId;
|
||||
|
||||
const uuid = require('./uuid').default;
|
||||
|
||||
if (!(await fs.pathExists(filePath))) throw new Error(_('Cannot access %s', filePath));
|
||||
|
@ -242,12 +244,16 @@ function shimInit(options = null) {
|
|||
defaultProps = defaultProps ? defaultProps : {};
|
||||
|
||||
let resourceId = defaultProps.id ? defaultProps.id : uuid.create();
|
||||
if (options.destinationResourceId) resourceId = options.destinationResourceId;
|
||||
if (isUpdate) resourceId = options.destinationResourceId;
|
||||
|
||||
let resource = options.destinationResourceId ? {} : Resource.new();
|
||||
let resource = isUpdate ? {} : Resource.new();
|
||||
resource.id = resourceId;
|
||||
|
||||
// When this is an update we auto-update the mime type, in case the
|
||||
// content type has changed, but we keep the title. It is still possible
|
||||
// to modify the title on update using defaultProps.
|
||||
resource.mime = mimeUtils.fromFilename(filePath);
|
||||
resource.title = basename(filePath);
|
||||
if (!isUpdate) resource.title = basename(filePath);
|
||||
|
||||
let fileExt = safeFileExtension(fileExtension(filePath));
|
||||
|
||||
|
@ -288,7 +294,7 @@ function shimInit(options = null) {
|
|||
const saveOptions = { isNew: true };
|
||||
if (options.userSideValidation) saveOptions.userSideValidation = true;
|
||||
|
||||
if (options.destinationResourceId) {
|
||||
if (isUpdate) {
|
||||
saveOptions.isNew = false;
|
||||
const tempPath = `${targetPath}.tmp`;
|
||||
await shim.fsDriver().move(targetPath, tempPath);
|
||||
|
|
Loading…
Reference in New Issue