From e666d67f04aa3acd8c4029fca39345f552e3cf2e Mon Sep 17 00:00:00 2001 From: simon louvet Date: Fri, 8 Jul 2022 16:12:26 +0200 Subject: [PATCH 1/4] fix accept for file --- .../packages/ldp/services/resource/actions/get.js | 14 +++++++++++--- yarn.lock | 4 ++++ 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 yarn.lock diff --git a/src/middleware/packages/ldp/services/resource/actions/get.js b/src/middleware/packages/ldp/services/resource/actions/get.js index 26126c743..072b89175 100644 --- a/src/middleware/packages/ldp/services/resource/actions/get.js +++ b/src/middleware/packages/ldp/services/resource/actions/get.js @@ -125,21 +125,29 @@ module.exports = { }); } - if ((result['@type'] === 'semapps:File' || result.type === 'semapps:File') && !forceSemantic) { + let triples = await this.bodyToTriples(result, accept); + + let type = triples.find(q=>['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'].includes(q.predicate.value)).object.value + + if (['http://semapps.org/ns/core#File','semapps:File'].includes(type) && !forceSemantic) { try { + const mimeType = triples.find(q=>['http://semapps.org/ns/core#mimeType','semapps:mimeType'].includes(q.predicate.value)).object.value + // Overwrite response type set by the api action - ctx.meta.$responseType = result['semapps:mimeType']; + ctx.meta.$responseType = mimeType; // Since files are currently immutable, we set a maximum browser cache age ctx.meta.$responseHeaders = { 'Cache-Control': 'public, max-age=31536000' }; - return fs.readFileSync(result['semapps:localPath']); + const localPath = triples.find(q=>['http://semapps.org/ns/core#localPath','semapps:localPath'].includes(q.predicate.value)).object.value + return fs.readFileSync(localPath); } catch (e) { throw new MoleculerError('File Not found', 404, 'NOT_FOUND'); } } else { return result; } + } else { throw new MoleculerError('Resource Not found', 404, 'NOT_FOUND'); } diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 000000000..fb57ccd13 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + From 0010931a31d2718e4913e3e48991361e0a234d6c Mon Sep 17 00:00:00 2001 From: simonLouvet Date: Fri, 8 Jul 2022 14:14:53 +0000 Subject: [PATCH 2/4] Automatic prettier --- .../packages/ldp/services/resource/actions/get.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/middleware/packages/ldp/services/resource/actions/get.js b/src/middleware/packages/ldp/services/resource/actions/get.js index 072b89175..a8b6f6e93 100644 --- a/src/middleware/packages/ldp/services/resource/actions/get.js +++ b/src/middleware/packages/ldp/services/resource/actions/get.js @@ -127,11 +127,14 @@ module.exports = { let triples = await this.bodyToTriples(result, accept); - let type = triples.find(q=>['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'].includes(q.predicate.value)).object.value + let type = triples.find(q => ['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'].includes(q.predicate.value)) + .object.value; - if (['http://semapps.org/ns/core#File','semapps:File'].includes(type) && !forceSemantic) { + if (['http://semapps.org/ns/core#File', 'semapps:File'].includes(type) && !forceSemantic) { try { - const mimeType = triples.find(q=>['http://semapps.org/ns/core#mimeType','semapps:mimeType'].includes(q.predicate.value)).object.value + const mimeType = triples.find(q => + ['http://semapps.org/ns/core#mimeType', 'semapps:mimeType'].includes(q.predicate.value) + ).object.value; // Overwrite response type set by the api action ctx.meta.$responseType = mimeType; @@ -139,7 +142,9 @@ module.exports = { ctx.meta.$responseHeaders = { 'Cache-Control': 'public, max-age=31536000' }; - const localPath = triples.find(q=>['http://semapps.org/ns/core#localPath','semapps:localPath'].includes(q.predicate.value)).object.value + const localPath = triples.find(q => + ['http://semapps.org/ns/core#localPath', 'semapps:localPath'].includes(q.predicate.value) + ).object.value; return fs.readFileSync(localPath); } catch (e) { throw new MoleculerError('File Not found', 404, 'NOT_FOUND'); @@ -147,7 +152,6 @@ module.exports = { } else { return result; } - } else { throw new MoleculerError('Resource Not found', 404, 'NOT_FOUND'); } From 6033def665ca923f3d90e986a8df2b174ea8fa5d Mon Sep 17 00:00:00 2001 From: simon louvet Date: Mon, 11 Jul 2022 12:35:21 +0200 Subject: [PATCH 3/4] optimisation --- .../ldp/services/resource/actions/get.js | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/middleware/packages/ldp/services/resource/actions/get.js b/src/middleware/packages/ldp/services/resource/actions/get.js index 072b89175..2d30a0067 100644 --- a/src/middleware/packages/ldp/services/resource/actions/get.js +++ b/src/middleware/packages/ldp/services/resource/actions/get.js @@ -124,30 +124,36 @@ module.exports = { } }); } + + if(!forceSemantic ){ + let triples = await this.bodyToTriples(result, accept); - let triples = await this.bodyToTriples(result, accept); - - let type = triples.find(q=>['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'].includes(q.predicate.value)).object.value + let type = triples.find(q=>['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'].includes(q.predicate.value)).object.value - if (['http://semapps.org/ns/core#File','semapps:File'].includes(type) && !forceSemantic) { - try { - const mimeType = triples.find(q=>['http://semapps.org/ns/core#mimeType','semapps:mimeType'].includes(q.predicate.value)).object.value + if (['http://semapps.org/ns/core#File','semapps:File'].includes(type)) { + try { + const mimeType = triples.find(q=>['http://semapps.org/ns/core#mimeType','semapps:mimeType'].includes(q.predicate.value)).object.value - // Overwrite response type set by the api action - ctx.meta.$responseType = mimeType; - // Since files are currently immutable, we set a maximum browser cache age - ctx.meta.$responseHeaders = { - 'Cache-Control': 'public, max-age=31536000' - }; - const localPath = triples.find(q=>['http://semapps.org/ns/core#localPath','semapps:localPath'].includes(q.predicate.value)).object.value - return fs.readFileSync(localPath); - } catch (e) { - throw new MoleculerError('File Not found', 404, 'NOT_FOUND'); + // Overwrite response type set by the api action + ctx.meta.$responseType = mimeType; + // Since files are currently immutable, we set a maximum browser cache age + ctx.meta.$responseHeaders = { + 'Cache-Control': 'public, max-age=31536000' + }; + const localPath = triples.find(q=>['http://semapps.org/ns/core#localPath','semapps:localPath'].includes(q.predicate.value)).object.value + return fs.readFileSync(localPath); + } catch (e) { + throw new MoleculerError('File Not found', 404, 'NOT_FOUND'); + } + } else { + return result; } } else { - return result; + return result } - + + + } else { throw new MoleculerError('Resource Not found', 404, 'NOT_FOUND'); } From d7a54f004ce1d1570673f680a79a8f6fc981d37b Mon Sep 17 00:00:00 2001 From: simonLouvet Date: Mon, 11 Jul 2022 10:40:07 +0000 Subject: [PATCH 4/4] Automatic prettier --- .../ldp/services/resource/actions/get.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/middleware/packages/ldp/services/resource/actions/get.js b/src/middleware/packages/ldp/services/resource/actions/get.js index 685e0d0ea..66acb5481 100644 --- a/src/middleware/packages/ldp/services/resource/actions/get.js +++ b/src/middleware/packages/ldp/services/resource/actions/get.js @@ -124,15 +124,18 @@ module.exports = { } }); } - - if(!forceSemantic ){ + + if (!forceSemantic) { let triples = await this.bodyToTriples(result, accept); - let type = triples.find(q=>['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'].includes(q.predicate.value)).object.value + let type = triples.find(q => ['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'].includes(q.predicate.value)) + .object.value; - if (['http://semapps.org/ns/core#File','semapps:File'].includes(type)) { + if (['http://semapps.org/ns/core#File', 'semapps:File'].includes(type)) { try { - const mimeType = triples.find(q=>['http://semapps.org/ns/core#mimeType','semapps:mimeType'].includes(q.predicate.value)).object.value + const mimeType = triples.find(q => + ['http://semapps.org/ns/core#mimeType', 'semapps:mimeType'].includes(q.predicate.value) + ).object.value; // Overwrite response type set by the api action ctx.meta.$responseType = mimeType; @@ -140,7 +143,9 @@ module.exports = { ctx.meta.$responseHeaders = { 'Cache-Control': 'public, max-age=31536000' }; - const localPath = triples.find(q=>['http://semapps.org/ns/core#localPath','semapps:localPath'].includes(q.predicate.value)).object.value + const localPath = triples.find(q => + ['http://semapps.org/ns/core#localPath', 'semapps:localPath'].includes(q.predicate.value) + ).object.value; return fs.readFileSync(localPath); } catch (e) { throw new MoleculerError('File Not found', 404, 'NOT_FOUND'); @@ -149,7 +154,7 @@ module.exports = { return result; } } else { - return result + return result; } } else { throw new MoleculerError('Resource Not found', 404, 'NOT_FOUND');