fix buffer

This commit is contained in:
Simon Larsen 2022-09-09 13:39:00 +01:00
parent 32fe596153
commit d0d17eee1c
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
12 changed files with 114 additions and 76 deletions

View File

@ -1,4 +1,5 @@
import { JSONObject } from '../JSON';
import Typeof from '../Typeof';
import HTTPResponse from './HTTPResponse';
export default class HTTPErrorResponse extends HTTPResponse<JSONObject> {
@ -10,6 +11,11 @@ export default class HTTPErrorResponse extends HTTPResponse<JSONObject> {
if (!this.data['error']) {
return '';
}
if (this.data['data'] && Typeof.String === this.data['data']) {
return this.data['data'] as string;
}
return this.data['error'] as string;
}
}

View File

@ -5,6 +5,7 @@ import {
JSONObject,
JSONObjectOrArray,
} from '../JSON';
import Typeof from '../Typeof';
export default class HTTPResponse<
T extends JSONObjectOrArray | BaseModel | Array<BaseModel>
@ -78,6 +79,10 @@ export default class HTTPResponse<
);
} else if (Array.isArray(data)) {
this.jsonData = JSONFunctions.deserializeArray(data as JSONArray);
} else if (Typeof.String === typeof data) {
this.jsonData = {
data,
};
} else {
this.jsonData = JSONFunctions.deserialize(data as JSONObject);
}

View File

@ -40,6 +40,7 @@ enum ObjectType {
Hostname = 'Hostname',
HashedString = 'HashedString',
DateTime = 'DateTime',
Buffer = 'Buffer',
InBetween = 'InBetween',
}
@ -51,6 +52,7 @@ export type JSONValue =
| Array<boolean>
| boolean
| JSONObject
| Uint8Array
| JSONArray
| Date
| Array<Date>
@ -148,7 +150,10 @@ export class JSONFunctions {
} else if (typeof val === Typeof.Number) {
return val;
} else if (ArrayBuffer.isView(val)) {
return val;
return {
_type: ObjectType.Buffer,
value: val as Uint8Array,
};
} else if (val && val instanceof Name) {
return {
_type: ObjectType.Name,
@ -240,6 +245,14 @@ export class JSONFunctions {
_type: ObjectType.DateTime,
value: OneUptimeDate.toString(val as Date).toString(),
};
} else if (
typeof val === Typeof.Object &&
(val as JSONObject)['_type'] &&
Object.keys(ObjectType).includes(
(val as JSONObject)['_type'] as string
)
) {
return val;
} else if (typeof val === Typeof.Object) {
return this.serialize(val as JSONObject);
}
@ -255,8 +268,17 @@ export class JSONFunctions {
val.toString().trim() === ''
) {
return val;
} else if (ArrayBuffer.isView(val)) {
return val;
} else if (
val &&
typeof val === Typeof.Object &&
(val as JSONObject)['_type'] &&
(val as JSONObject)['value'] &&
((val as JSONObject)['value'] as JSONObject)['type'] &&
((val as JSONObject)['value'] as JSONObject)['type'] ===
ObjectType.Buffer &&
((val as JSONObject)['_type'] as string) === ObjectType.Buffer
) {
return Buffer.from((val as JSONObject)['value'] as Uint8Array);
} else if (typeof val === Typeof.Number) {
return val;
} else if (val instanceof DatabaseProperty) {

View File

@ -4,7 +4,6 @@ import BaseModel from 'Common/Models/BaseModel';
import Query from '../../Utils/ModelAPI/Query';
import ModelAPI, { RequestOptions } from '../../Utils/ModelAPI/ModelAPI';
import HTTPErrorResponse from 'Common/Types/API/HTTPErrorResponse';
import { JSONObject } from 'Common/Types/JSON';
export interface ComponentProps<TBaseModel extends BaseModel> {
alertType: AlertType;
@ -43,9 +42,8 @@ const CounterModelAlert: Function = <TBaseModel extends BaseModel>(
} catch (err) {
try {
setError(
((err as HTTPErrorResponse).data as JSONObject)[
'error'
] as string
(err as HTTPErrorResponse).message ||
'Server Error. Please try again'
);
} catch (e) {
setError('Server Error. Please try again');

View File

@ -14,6 +14,7 @@ import ComponentLoader from '../ComponentLoader/ComponentLoader';
import Icon, { IconProp, SizeProp, ThickProp } from '../Icon/Icon';
import { White } from 'Common/Types/BrandColors';
import HTTPResponse from 'Common/Types/API/HTTPResponse';
import HTTPErrorResponse from 'Common/Types/API/HTTPErrorResponse';
export interface ComponentProps {
initialValue?: undefined | Array<FileModel>;
@ -34,7 +35,7 @@ const FilePicker: FunctionComponent<ComponentProps> = (
props: ComponentProps
): ReactElement => {
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string>('');
const [filesModel, setFilesModel] = useState<Array<FileModel>>([]);
useEffect(() => {
@ -51,39 +52,50 @@ const FilePicker: FunctionComponent<ComponentProps> = (
accept: {
'image/*': [],
},
onDrop: (async (acceptedFiles: Array<File>) => {
onDrop: async (acceptedFiles: Array<File>) => {
setIsLoading(true);
try {
if (props.readOnly) {
return;
}
if (props.readOnly) {
return;
// Upload these files.
const filesResult: Array<FileModel> = [];
for (const acceptedFile of acceptedFiles) {
const fileModel: FileModel = new FileModel();
fileModel.name = acceptedFile.name;
const fileBuffer = Buffer.from(
await getBase64(acceptedFile),
'base64'
);
fileModel.file = fileBuffer;
fileModel.isPublic = false;
fileModel.type = acceptedFile.type as MimeType;
const result: HTTPResponse<FileModel> =
(await ModelAPI.create<FileModel>(
fileModel,
CommonURL.fromURL(FILE_URL).addRoute('/file')
)) as HTTPResponse<FileModel>;
filesResult.push(result.data as FileModel);
}
setFilesModel(filesResult);
props.onBlur && props.onBlur();
props.onChange && props.onChange(filesModel);
} catch (err) {
try {
setError(
(err as HTTPErrorResponse).message ||
'Server Error. Please try again'
);
} catch (e) {
setError('Server Error. Please try again');
}
}
// Upload these files.
const filesResult: Array<FileModel> = [];
for (const acceptedFile of acceptedFiles) {
const fileModel: FileModel = new FileModel();
fileModel.name = acceptedFile.name;
const fileBuffer = Buffer.from(
await getBase64(acceptedFile),
'base64'
);
fileModel.file = fileBuffer;
fileModel.isPublic = false;
fileModel.type = acceptedFile.type as MimeType;
const result: HTTPResponse<FileModel> = await ModelAPI.create<FileModel>(
fileModel,
CommonURL.fromURL(FILE_URL).addRoute('/file')
) as HTTPResponse<FileModel>;
filesResult.push(result.data as FileModel);
}
setFilesModel(filesResult);
props.onBlur && props.onBlur();
props.onChange && props.onChange(filesModel);
setIsLoading(false);
})
},
});
const getBase64 = (file: File): Promise<string> => {
@ -105,10 +117,9 @@ const FilePicker: FunctionComponent<ComponentProps> = (
return <></>;
}
const blob = new Blob(
[new Uint8Array((file.file as any).data).buffer],
{ type: file.type as string }
);
const blob = new Blob([file.file as Uint8Array], {
type: file.type as string,
});
const url: string = URL.createObjectURL(blob);
return (
<div key={file.name} className="file-picker-thumb">
@ -158,13 +169,18 @@ const FilePicker: FunctionComponent<ComponentProps> = (
})}
>
<input {...getInputProps()} />
{!props.placeholder && (
{!props.placeholder && !error && (
<p className="file-picker-placeholder">
Drag and drop some files here, or click to
select files.
</p>
)}
{props.placeholder && (
{error && (
<p className="file-picker-placeholder">
{error}
</p>
)}
{props.placeholder && !error && (
<p className="file-picker-placeholder">
{props.placeholder}
</p>

View File

@ -282,9 +282,8 @@ const ModelForm: Function = <TBaseModel extends BaseModel>(
} catch (err) {
try {
setError(
((err as HTTPErrorResponse).data as JSONObject)[
'error'
] as string
(err as HTTPErrorResponse).message ||
'Server Error. Please try again'
);
} catch (e) {
setError('Server Error. Please try again');
@ -305,9 +304,9 @@ const ModelForm: Function = <TBaseModel extends BaseModel>(
} catch (err) {
let error: string = '';
try {
error = ((err as HTTPErrorResponse).data as JSONObject)[
'error'
] as string;
error =
(err as HTTPErrorResponse).message ||
'Server Error. Please try again';
} catch (e) {
error = 'Server Error. Please try again';
}
@ -386,9 +385,8 @@ const ModelForm: Function = <TBaseModel extends BaseModel>(
}
} catch (err) {
setError(
((err as HTTPErrorResponse).data as JSONObject)[
'error'
] as string
(err as HTTPErrorResponse).message ||
'Server Error. Please try again'
);
}

View File

@ -1,6 +1,5 @@
import BaseModel from 'Common/Models/BaseModel';
import HTTPErrorResponse from 'Common/Types/API/HTTPErrorResponse';
import { JSONObject } from 'Common/Types/JSON';
import ObjectID from 'Common/Types/ObjectID';
import React, { ReactElement, useState } from 'react';
import ModelAPI from '../../Utils/ModelAPI/ModelAPI';
@ -35,9 +34,8 @@ const ModelDelete: Function = <TBaseModel extends BaseModel>(
} catch (err) {
try {
setError(
((err as HTTPErrorResponse).data as JSONObject)[
'error'
] as string
(err as HTTPErrorResponse).message ||
'Server Error. Please try again'
);
} catch (e) {
setError('Server Error. Please try again');

View File

@ -201,9 +201,9 @@ const ModelDetail: Function = <TBaseModel extends BaseModel>(
} catch (err) {
let error: string = '';
try {
error = ((err as HTTPErrorResponse).data as JSONObject)[
'error'
] as string;
error =
(err as HTTPErrorResponse).message ||
'Server Error. Please try again';
} catch (e) {
error = 'Server Error. Please try again';
}

View File

@ -207,9 +207,8 @@ const ModelTable: Function = <TBaseModel extends BaseModel>(
} catch (err) {
try {
setError(
((err as HTTPErrorResponse).data as JSONObject)[
'error'
] as string
(err as HTTPErrorResponse).message ||
'Server Error. Please try again'
);
} catch (e) {
setError('Server Error. Please try again');
@ -309,9 +308,8 @@ const ModelTable: Function = <TBaseModel extends BaseModel>(
} catch (err) {
try {
setTableFilterError(
((err as HTTPErrorResponse).data as JSONObject)[
'error'
] as string
(err as HTTPErrorResponse).message ||
'Server Error. Please try again'
);
} catch (e) {
setTableFilterError('Server Error. Please try again');
@ -359,9 +357,8 @@ const ModelTable: Function = <TBaseModel extends BaseModel>(
} catch (err) {
try {
setError(
((err as HTTPErrorResponse).data as JSONObject)[
'error'
] as string
(err as HTTPErrorResponse).message ||
'Server Error. Please try again'
);
} catch (e) {
setError('Server Error. Please try again');

View File

@ -16,7 +16,6 @@ import DayUptimeGraph, { Event } from '../Graphs/DayUptimeGraph';
import { Green } from 'Common/Types/BrandColors';
import ErrorMessage from '../ErrorMessage/ErrorMessage';
import HTTPErrorResponse from 'Common/Types/API/HTTPErrorResponse';
import { JSONObject } from 'Common/Types/JSON';
import useAsyncEffect from 'use-async-effect';
export interface ComponentProps {
@ -96,9 +95,8 @@ const MonitorUptimeGraph: FunctionComponent<ComponentProps> = (
} catch (err) {
try {
setError(
((err as HTTPErrorResponse).data as JSONObject)[
'error'
] as string
(err as HTTPErrorResponse).message ||
'Server Error. Please try again'
);
} catch (e) {
setError('Server Error. Please try again');

View File

@ -66,7 +66,6 @@ import Logout from './Pages/Logout/Logout';
import ModelAPI, { ListResult } from 'CommonUI/src/Utils/ModelAPI/ModelAPI';
import Project from 'Model/Models/Project';
import HTTPErrorResponse from 'Common/Types/API/HTTPErrorResponse';
import { JSONObject } from 'Common/Types/JSON';
const App: FunctionComponent = () => {
Navigation.setNavigateHook(useNavigate());
@ -120,9 +119,8 @@ const App: FunctionComponent = () => {
setProjects(result.data);
} catch (err) {
setError(
((err as HTTPErrorResponse).data as JSONObject)[
'error'
] as string
(err as HTTPErrorResponse).message ||
'Server Error. Please try again'
);
}

View File

@ -131,6 +131,8 @@ server {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://file/;
client_max_body_size 50M;
}
location /api {