Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload single file #1111

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/frontend/packages/semantic-data-provider/dist/index.es.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import urlJoin from 'url-join';
import getOne from './getOne';
import uploadAllFiles from '../utils/uploadAllFiles';
import uploadAllFiles, { getSlugWithExtension, isFile } from '../utils/uploadAllFiles';
import findContainersWithTypes from '../utils/findContainersWithTypes';
import { defaultToArray } from "@semapps/auth-provider/src/utils";

const createMethod = config => async (resourceId, params) => {
const { dataServers, resources, httpClient, jsonContext } = config;
Expand Down Expand Up @@ -33,30 +34,56 @@ const createMethod = config => async (resourceId, params) => {
}

if (params.data) {
if (dataModel.fieldsMapping?.title) {
if (Array.isArray(dataModel.fieldsMapping.title)) {
headers.set('Slug', dataModel.fieldsMapping.title.map(f => params.data[f]).join(' '));
} else {
headers.set('Slug', params.data[dataModel.fieldsMapping.title]);
if (defaultToArray(dataModel.types).includes('semapps:File')) {
const keys = Object.keys(params.data);
if (keys.length !== 1 && !isFile(params.data[keys[0]])) {
throw new Error('For ressources of types semapps:File, you must provide a single file value');
}
}

// Upload files, if there are any
params.data = await uploadAllFiles(params.data, config);
const { rawFile } = params.data[keys[0]];

const { headers: responseHeaders } = await httpClient(containerUri, {
method: 'POST',
headers,
body: JSON.stringify({
'@context': jsonContext,
'@type': dataModel.types,
...params.data
})
});
// We must sluggify the file name, because we can't use non-ASCII characters in the header
// However we keep the extension apart (if it exists) so that it is not replaced with a -
// TODO let the middleware guess the extension based on the content type
headers.set('Slug', getSlugWithExtension(rawFile.name));
headers.set('Content-Type', rawFile.type);

// Retrieve newly-created resource
const resourceUri = responseHeaders.get('Location');
return await getOne(config)(resourceId, { id: resourceUri });
const response = await httpClient(containerUri, {
method: 'POST',
headers,
body: rawFile
});

// TODO fetch file info when it will be possible ?
// https://github.com/assemblee-virtuelle/semapps/issues/1107
const fileUri = response.headers.get('Location');
return { data: { id: fileUri } };
} else {
if (dataModel.fieldsMapping?.title) {
if (Array.isArray(dataModel.fieldsMapping.title)) {
headers.set('Slug', dataModel.fieldsMapping.title.map(f => params.data[f]).join(' '));
} else {
headers.set('Slug', params.data[dataModel.fieldsMapping.title]);
}
}

// Upload files, if there are any
params.data = await uploadAllFiles(params.data, config);

const response = await httpClient(containerUri, {
method: 'POST',
headers,
body: JSON.stringify({
'@context': jsonContext,
'@type': dataModel.types,
...params.data
})
});

// Retrieve newly-created resource
const resourceUri = response.headers.get('Location');
return await getOne(config)(resourceId, { id: resourceUri });
}
} else if (params.id) {
headers.set('Content-Type', 'application/sparql-update');

Expand Down