Skip to content

Commit

Permalink
Api: Fixed updating resource content
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Apr 9, 2022
1 parent 8077117 commit f2bfa30
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/app-cli/app/command-apidoc.js
Expand Up @@ -313,10 +313,14 @@ async function fetchAllNotes() {
lines.push('');
lines.push('\tcurl -F \'data=@/path/to/file.jpg\' -F \'props={"title":"my resource title"}\' http://localhost:41184/resources');
lines.push('');
lines.push('Or to **update** a resource:');
lines.push('To **update** the resource content, you can make a PUT request with the same arguments:');
lines.push('');
lines.push('\tcurl -X PUT -F \'data=@/path/to/file.jpg\' -F \'props={"title":"my modified title"}\' http://localhost:41184/resources/8fe1417d7b184324bf6b0122b76c4696');
lines.push('');
lines.push('Or if you only need to update the resource properties (title, etc.), without changing the content, you can make a regular PUT request:');
lines.push('');
lines.push('\tcurl -X PUT --data \'{"title": "My new title"}\' http://localhost:41184/resources/8fe1417d7b184324bf6b0122b76c4696');
lines.push('');
lines.push('The "data" field is required, while the "props" one is not. If not specified, default values will be used.');
lines.push('');
lines.push('**From a plugin** the syntax to create a resource is also a bit special:');
Expand Down
24 changes: 22 additions & 2 deletions packages/lib/services/rest/Api.test.ts
Expand Up @@ -9,6 +9,7 @@ import Tag from '../../models/Tag';
import NoteTag from '../../models/NoteTag';
import ResourceService from '../../services/ResourceService';
import SearchEngine from '../../services/searchengine/SearchEngine';
import { ResourceEntity } from '../database/types';

const createFolderForPagination = async (num: number, time: number) => {
await Folder.save({
Expand Down Expand Up @@ -354,7 +355,7 @@ describe('services_rest_Api', function() {
},
]);

const resourceV1 = (await Resource.all())[0];
const resourceV1: ResourceEntity = (await Resource.all())[0];

await msleep(1);

Expand All @@ -366,7 +367,7 @@ describe('services_rest_Api', function() {
},
]);

const resourceV2 = (await Resource.all())[0];
const resourceV2: ResourceEntity = (await Resource.all())[0];

expect(resourceV2.title).toBe('resource mod');
expect(resourceV2.mime).toBe('image/png');
Expand All @@ -378,6 +379,25 @@ describe('services_rest_Api', function() {
expect(resourceV2.size).toBe((await shim.fsDriver().stat(Resource.fullPath(resourceV2))).size);
}));

it('should update resource properties', (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, JSON.stringify({
title: 'my new title',
}));

const resourceV2: ResourceEntity = (await Resource.all())[0];

expect(resourceV2.title).toBe('my new title');
expect(resourceV2.mime).toBe(resourceV1.mime);
}));

it('should delete resources', (async () => {
const f = await Folder.save({ title: 'mon carnet' });

Expand Down
12 changes: 11 additions & 1 deletion packages/lib/services/rest/routes/resources.ts
Expand Up @@ -50,7 +50,17 @@ export default async function(request: Request, id: string = null, link: string
if (request.method === RequestMethod.POST || request.method === RequestMethod.PUT) {
const isUpdate = request.method === RequestMethod.PUT;

if (!request.files.length) throw new ErrorBadRequest('Resource cannot be created without a file');
if (!request.files.length) {
if (request.method === RequestMethod.PUT) {
// In that case, we don't try to update the resource blob, we
// just update the properties.
return defaultAction(BaseModel.TYPE_RESOURCE, request, id, link);
} else {
// If it's a POST request, the file content is required.
throw new ErrorBadRequest('Resource cannot be created without a file');
}
}

if (isUpdate && !id) throw new ErrorBadRequest('Missing resource ID');
const filePath = request.files[0].path;
const defaultProps = request.bodyJson(readonlyProperties(request.method));
Expand Down

0 comments on commit f2bfa30

Please sign in to comment.