Skip to content

Commit

Permalink
Blokh/feat/implment-s-3-filemanager (#284)
Browse files Browse the repository at this point in the history
* feat: added S3 client

added S3 client.

* feat: removed unnecessary variables

* feat: merged with dev

* update migration names and timeline

* refactor(fixed pr comments): fixed pr comments

* updated is_prefix to use boolean

* update migration names and timeline

* updated after PR comments, refactored names, tested logics

* fixed error throwing of aws

* merged with dev

* removed unnecessary logs

* updated private methods to be in the correct format

* removed unnecessary export

* Blokh/feat/File Service + File Provider Interface (#282)

* process to process env to be more explicit

* generated interface for file proviers. finalized S3 provier and http provider> TBD

* added error with name to common

* update common version

* generate local mananager service file provider

* generate file service logic

* generate first streamable handling process

* feat(file service): finalized file service provider and stracture

* finalized file service module

* finalized file service module

* finalize file service - began working ontest

* finalized file service via streamingm

* finished the upload logic for local file manager, tested everything, updated package json

---------

Co-authored-by: blokh <danielb@ballerine.com>

* added more fixes

---------

Co-authored-by: blokh <danielb@ballerine.com>
  • Loading branch information
Blokh and blokh committed May 17, 2023
1 parent 3a4710d commit be5c9bc
Show file tree
Hide file tree
Showing 38 changed files with 1,763 additions and 63 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-lobsters-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ballerine/common': minor
---

added error with name validation
4 changes: 4 additions & 0 deletions apps/backoffice-v2/src/api/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,9 @@ export const endpoints = {
endpoint: (fileId: string) => `storage/${fileId}`,
method: Method.GET,
},
fileContentById: {
endpoint: (fileId: string) => `storage/content/${fileId}`,
method: Method.GET,
},
},
} as const satisfies IEndpoint;
15 changes: 11 additions & 4 deletions apps/backoffice-v2/src/api/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ import { handleZodError } from '../utils/handle-zod-error/handle-zod-error';
import { apiClient } from './api-client';
import { endpoints } from './endpoints';

import { blobToBase64 } from '../utils/blob-to-base64/blob-to-base64';
import { blobToBase64 } from '../utils/fetch-blob-to-base64/fetch-blob-to-base64';
import { FileInfoSchema } from '../lib/zod/schemas/file-info';

export const storage = {
fileById: async (fileId: string) => {
const [blob, error] = await apiClient({
const [fileInfoResponse, fetchFileError] = await apiClient({
endpoint: endpoints.storage.fileById.endpoint(fileId),
method: endpoints.storage.fileById.method,
schema: FileInfoSchema,
});
const fileInfo = handleZodError(fetchFileError, fileInfoResponse);

const [blob, fetchFileContentError] = await apiClient({
endpoint: endpoints.storage.fileContentById.endpoint(fileInfo.id),
method: endpoints.storage.fileById.method,
schema: z.instanceof(Blob),
isBlob: true,
});

const data = handleZodError(error, blob);
const data = handleZodError(fetchFileContentError, blob);

return blobToBase64(data);
},
Expand Down
2 changes: 2 additions & 0 deletions apps/backoffice-v2/src/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from 'zod';
import { EndUserByIdSchema, EndUsersListSchema } from '../lib/zod/schemas/end-users';
import { FileInfoSchema } from '../lib/zod/schemas/file-info';
import { AuthenticatedUserSchema } from '../lib/zod/schemas/authenticated-user';
import { UsersListSchema } from '../lib/zod/schemas/users';

Expand All @@ -18,3 +19,4 @@ export type TCaseManagementState = {
unassignedEnabled?: boolean;
actionButtonsEnabled: boolean;
};
export type TFileInfo = z.infer<typeof FileInfoSchema>;
8 changes: 8 additions & 0 deletions apps/backoffice-v2/src/lib/zod/schemas/file-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from 'zod';
import { ObjectWithIdSchema } from '../utils/object-with-id';
export const FileInfoSchema = ObjectWithIdSchema.extend({
userId: z.string().nullable().default(''),
fileNameOnDisk: z.string().default(''),
uri: z.string().nullable().default(''),
fileNameInBucket: z.string().default(''),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { isObject } from '../is-object/is-object';
import { IErrorWithName } from './interfaces';

export const isErrorWithName = (error: unknown): error is IErrorWithName => {
return isObject(error) && 'name' in error && typeof error.name === 'string';
};
23 changes: 20 additions & 3 deletions examples/headless-example/src/components/RemoteImage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,30 @@
});
};
const isFileSourcePublic = fileInfo => {
return fileInfo.uri.includes('https') && !fileInfo.fileNameInBucket;
};
onMount(async () => {
if (!id) return;
const data = await fetchBlob<Blob>(`http://localhost:3000/api/external/storage/${id}`);
const base64 = await blobToBase64(data);
const response = await fetch(`http://localhost:3000/api/external/storage/${id}`);
if (!response.ok) {
throw new Error(`Error fetching fileInfo: ${response.statusText}`);
}
const fileInfo = await response.json();
if (isFileSourcePublic(fileInfo)) {
src = fileInfo.uri;
} else {
const streamedFile = await fetchBlob<Blob>(
`http://localhost:3000/api/external/storage/content/${id}`,
);
const base64 = await blobToBase64(streamedFile);
src = base64?.replace(/application\/octet-stream/gi, fileType);
src = base64?.replace(/application\/octet-stream/gi, fileType);
}
});
</script>

Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": false,
"name": "@ballerine/common",
"author": "Ballerine <dev@ballerine.com>",
"version": "0.4.3",
"version": "0.4.4",
"description": "common",
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js",
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export {
handlePromise,
isEmptyObject,
isErrorWithMessage,
isErrorWithName,
isFunction,
isNullish,
isObject,
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { handlePromise } from './handle-promise';
export { isEmptyObject } from './is-empty-object';
export { isErrorWithMessage } from './is-error-with-message';
export { isErrorWithName } from './is-error-with-message';
export { isFunction } from './is-function';
export { isNullish } from './is-nullish';
export { isObject } from './is-object';
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/utils/is-error-with-message/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { isErrorWithMessage } from './is-error-with-message';
export { isErrorWithName } from './is-error-with-name';
4 changes: 4 additions & 0 deletions packages/common/src/utils/is-error-with-message/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interface IErrorWithMessage extends Error {
message: string;
}

export interface IErrorWithName extends Error {
name: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { isObject } from '@/utils';
import { IErrorWithName } from './interfaces';

export const isErrorWithName = (error: unknown): error is IErrorWithName => {
return isObject(error) && 'name' in error && typeof error.name === 'string';
};
Loading

0 comments on commit be5c9bc

Please sign in to comment.