Skip to content

Commit

Permalink
feat: header file-content resulting in server responding with file-co…
Browse files Browse the repository at this point in the history
…ntent value as the body. also addeded header for storage to inform the client if the organization has a storage applied or not
  • Loading branch information
frankpagan committed Aug 16, 2023
1 parent 4f1fa9c commit 29c68a2
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
/********************************************************************************
* Copyright (C) 2022 CoCreate LLC and others.
* Copyright (C) 2023 CoCreate and Contributors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SPDX-License-Identifier: MIT
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
********************************************************************************/

// Commercial Licensing Information:
// For commercial use of this software without the copyleft provisions of the AGPLv3,
// you must obtain a commercial license from CoCreate LLC.
// For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.

const { URL } = require('url');

const organizations = new Map();
Expand Down Expand Up @@ -42,12 +58,6 @@ class CoCreateFileSystem {

server.on('request', async (req, res) => {
try {
const fileContent = req.headers['File-Content']
if (fileContent) {
res.writeHead(200, { 'Content-Type': req.headers['Content-Type'] });
return res.end(fileContent);
}

const valideUrl = new URL(`http://${req.headers.host}${req.url}`);
const hostname = valideUrl.hostname;

Expand All @@ -67,11 +77,17 @@ class CoCreateFileSystem {
if (!org || !org.object || !org.object[0]) {
hostNotFound = hostNotFound || 'An organization could not be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id
res.writeHead(404, { 'Content-Type': 'text/plain' });
if (org.storage === false && org.error)
res.setHeader('storage', 'false')
else
res.setHeader('storage', 'true')

return res.end(hostNotFound);
} else {
organization = { _id: org.object[0]._id }
organization = { _id: org.object[0]._id, storage: !!org.object[0].storage }
organizations.set(hostname, organization)
}

}

let organization_id = organization._id
Expand Down Expand Up @@ -110,6 +126,16 @@ class CoCreateFileSystem {
data.organization_id = process.env.organization_id

let file = await crud.send(data);
if (file.storage === false && file.error)
res.setHeader('storage', 'false')
else
res.setHeader('storage', 'true')

const fileContent = req.headers['File-Content']
if (fileContent && !pathname.startsWith('/superadmin')) {
res.writeHead(200, { 'Content-Type': req.headers['Content-Type'] });
return res.end(fileContent);
}

if (!file || !file.object || !file.object[0]) {
data.filter.query[1].value = '/404.html'
Expand Down

0 comments on commit 29c68a2

Please sign in to comment.