Skip to content

Commit

Permalink
0.0.564
Browse files Browse the repository at this point in the history
  • Loading branch information
ivansglazunov committed Sep 24, 2024
1 parent 360f3e2 commit 7f93b0d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 34 deletions.
6 changes: 4 additions & 2 deletions imports/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2412,8 +2412,10 @@ export class DeepClient<L extends Link<Id> = Link<Id>> implements DeepClientInst
return q;
}

url(target: 'deeplinks' | 'gql') {
return target === 'gql' ? this.client.path : `http${this.client.ssl ? 's' : ''}://${this.client.path.slice(0, this.client.path.indexOf('/gql'))}`
url(target: 'deeplinks' | 'gql' | Id) {
return target === 'gql' ? this.client.path : // gql
(target !== 'deeplinks' && typeof(target) === 'number') ? `${this.url('deeplinks')}/file?linkId=${target}` : // file
`http${this.client.ssl ? 's' : ''}://${this.client.path.slice(0, this.client.path.indexOf('/gql'))}`; // deeplinks
}
}

Expand Down
42 changes: 23 additions & 19 deletions imports/files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,17 @@ export function useFiles({
const type_id = useMemo(() => _type_id || deep.idLocal('@deep-foundation/core', 'AsyncFile'), [_type_id]);
const onDrop = async (files, a, event) => {
let _prevent = prevent;
_onDrop && _onDrop(files, a, event, () => _prevent = true);
if (!_prevent) for (const file of files) {
const result = await deep.insert({
file,
type_id,
containerId,
...insert,
});
onInsert && onInsert(result?.data?.[0]?.id, file, a, event);
_onDrop && _onDrop(files, a, event, () => { _prevent = true });
if (!_prevent) {
for (const file of files) {
const result = await deep.insert({
file,
type_id,
containerId,
...insert,
});
onInsert && onInsert(result?.data?.[0]?.id, file, a, event);
}
}
};
const dropzone = dz.useDropzone({
Expand All @@ -120,7 +122,7 @@ export function useFiles({
return dropzone;
}

export function base64ToFile(dataurl, filename) {
export async function base64ToFile(dataurl, filename): Promise<File> {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[arr.length - 1]),
Expand All @@ -132,13 +134,15 @@ export function base64ToFile(dataurl, filename) {
return new File([u8arr], filename, { type: mime });
}

export function fileToBase64(file) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
console.log(reader.result);
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
export async function fileToBase64(file): Promise<string> {
return new Promise((res, rej) => {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
res(String(reader.result));
};
reader.onerror = function (error) {
rej(error);
};
});
}
32 changes: 20 additions & 12 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ app.get(['/file'], createProxyMiddleware((pathname, req) => {
console.log('/file get proxy', 'req.hostname', req.hostname);
console.log('/file get proxy', 'req.url', req.url);
console.log('/file get proxy', 'req.query.linkId', req.query.linkId)
console.log('/file get proxy', 'req.query.token', req.query.token)
req.params
const headers = req.headers;
console.log('/file get proxy', 'headers', headers);
Expand All @@ -124,12 +125,14 @@ app.get(['/file'], createProxyMiddleware((pathname, req) => {
if (tokenCookie) {
token = JSON.parse(tokenCookie)?.value;
console.log('/file get proxy', 'cookie token', token);
if (token) {
req.headers.authorization = `Bearer ${token}`;
}
console.log('/file get proxy', 'cookie token is set as header token');
} else {
if (req.query.token) token = req.query.token as string;
}
}
if (token) {
req.headers.authorization = `Bearer ${token}`;
}
console.log('/file get proxy', 'result token', token);

const deep = makeDeepClient(token);
Expand Down Expand Up @@ -161,17 +164,22 @@ app.post('/file', async (req, res, next) => {
const cookies = req.cookies;
console.log('/file post proxy', 'cookies', JSON.stringify(cookies, null, 2));
let userId;
let linkId;
let linkId = +(headers['linkId'] || headers['linkid']) || +req.query?.linkId;
console.log('/file post proxy', 'req.query.linkId', req.query.linkId)
console.log('/file post proxy', 'req.query.token', req.query.token)
if (headers.authorization) {
try {
const claims = atob(`${headers['authorization'] ? headers['authorization'] : headers['Authorization']}`.split(' ')[1].split('.')[1]);
userId = +(JSON.parse(claims)['https://hasura.io/jwt/claims']['x-hasura-user-id']);
linkId = +(headers['linkId'] || headers['linkid']);
console.log('/file post proxy','linkId',linkId);
} catch (e) {
const serializedError = serializeError(e);
console.log('/file post proxy','error: ', JSON.stringify(serializedError, null, 2));
const claims = atob(`${headers['authorization'] ? headers['authorization'] : headers['Authorization']}`.split(' ')[1].split('.')[1]);
userId = +(JSON.parse(claims)['https://hasura.io/jwt/claims']['x-hasura-user-id']);
console.log('/file post proxy','linkId',linkId);
} catch (e) {
const serializedError = serializeError(e);
console.log('/file post proxy','error: ', JSON.stringify(serializedError, null, 2));
}
} else if (req.query.token) {
userId = +(await deep.jwt({ token: req.query.token as string })).linkId
}
if (!userId) res.status(403).send('Update CAN NOT be processes');
if (!userId) res.status(403).send('!user (req.query.token || headers.authorization)');
const canResult = await deep.can(linkId, userId, deep.idLocal('@deep-foundation/core', 'AllowUpdate')) || await deep.can(null, userId, deep.idLocal('@deep-foundation/core', 'AllowAdmin'));
console.log('/file post proxy','can', await deep.can(linkId, userId, deep.idLocal('@deep-foundation/core', 'AllowUpdate')), 'isAdmin', await deep.can(null, userId, deep.idLocal('@deep-foundation/core', 'AllowAdmin')));
console.log('/file post proxy','userId', userId, typeof(userId));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@deep-foundation/deeplinks",
"version": "0.0.563",
"version": "0.0.564",
"license": "Unlicense",
"type": "module",
"main": "import.js",
Expand Down

0 comments on commit 7f93b0d

Please sign in to comment.