Skip to content

Commit

Permalink
make file uploads pass
Browse files Browse the repository at this point in the history
  • Loading branch information
maany committed Aug 7, 2024
1 parent 9b6d0ab commit 03f039f
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 192 deletions.
5 changes: 3 additions & 2 deletions src/lib/infrastructure/client/config/ioc/client-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import "reflect-metadata";
import { Container } from "inversify";
import { CONTROLLERS, REPOSITORY, TRPC } from "./client-ioc-symbols";
import { api } from "~/lib/infrastructure/client/trpc/react-api";
import { api as vanilla } from "~/lib/infrastructure/client/trpc/vanilla-api";
import KernelFileClientRepository from "../../repository/kernel-planckster-file-repository";
import BrowserFileUploadController from "../../controller/browser-file-upload-controller";

const clientContainer = new Container();

/** GATEWAYS */
clientContainer.bind(TRPC.REACT_CLIENT_COMPONENTS_API).toConstantValue(api);

clientContainer.bind(TRPC.VANILLA_CLIENT).toConstantValue(vanilla);
/** REPOSITORY */
clientContainer.bind(REPOSITORY.KERNEL_FILE_REPOSITORY).to(KernelFileClientRepository);

/** CONTROLLER */
clientContainer.bind(CONTROLLERS.KERNEL_FILE_UPLOAD_CONTROLLER).to(BrowserFileUploadController);

export default clientContainer;
export default clientContainer;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const SIGNALS = {

export const TRPC = {
REACT_CLIENT_COMPONENTS_API: Symbol("TRPC_REACT_CLIENT_API"),
VANILLA_CLIENT: Symbol("TRPC_VANILLA_CLIENT"),
}

export const REPOSITORY = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,50 @@ import type KernelFileRepositoryOutputPort from "~/lib/core/ports/secondary/kern
import type{ GetDownloadSignedUrlDTO, GetUploadSignedUrlDTO } from "~/lib/core/dto/kernel-file-repository-dto";
import type{ UploadFileDTO, DownloadFileDTO } from "~/lib/core/dto/file-repository-dto";
import type{ LocalFile, RemoteFile } from "~/lib/core/entity/file";
import type { TClientComponentAPI } from "../trpc/react-api";
import { inject, injectable } from "inversify";
import { TRPC } from "../config/ioc/client-ioc-symbols";
import type { TVanillaAPI } from "../trpc/vanilla-api";

@injectable()
export default class KernelFileClientRepository implements KernelFileRepositoryOutputPort{
constructor(
private api: TClientComponentAPI
@inject(TRPC.VANILLA_CLIENT) private api: TVanillaAPI
){}

async getUploadSignedUrl(protocol: string, relativePath: string): Promise<GetUploadSignedUrlDTO> {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const getUploadSignedUrlQuery = this.api.kernel.sourceData.getUploadSignedUrl.useQuery({
const response = await this.api.kernel.sourceData.getUploadSignedUrl.query({
protocol: protocol,
relativePath: relativePath,
});


if (getUploadSignedUrlQuery.error) {
if (!response) {
return {
success: false,
data: {
message: `An error occurred. TRPC server query failed: ${JSON.stringify(getUploadSignedUrlQuery.error)}`,
message: `Could not get signed url from the TRPC server.`,
operation: "kp-file-repository#get-upload-signed-url",
}
}
}
if(!response.success) {
return {
success: false,
data: {
message: `An error occurred. Error: ${JSON.stringify(response.data, null, 2)}`,
operation: "kp#get-upload-signed-url",
}
}
}

const signedUrl = response.data;

const signedUrl = getUploadSignedUrlQuery.data;

if (!signedUrl || signedUrl.length === 0 || typeof signedUrl !== 'string') {
if (!signedUrl || typeof signedUrl !== 'string') {
return {
success: false,
data: {
message: `An error occurred. Signed URL received from TRPC server is null: ${getUploadSignedUrlQuery.error}`,
message: `An error occurred. Signed URL received from TRPC server is null: ${JSON.stringify(response)}`,
operation: "kp#get-upload-signed-url",
}
}
Expand Down Expand Up @@ -64,54 +76,55 @@ export default class KernelFileClientRepository implements KernelFileRepositoryO
}

async getDownloadSignedUrl(protocol: string, relativePath: string): Promise<GetDownloadSignedUrlDTO> {
try {

const getDownloadSignedUrlQuery = this.api.kernel.sourceData.getDownloadSignedUrl.useQuery({
protocol: protocol,
relativePath: relativePath,
});

if (getDownloadSignedUrlQuery.error) {
return {
success: false,
data: {
message: `An error occurred. TRPC server query failed: ${JSON.stringify(getDownloadSignedUrlQuery.error)}`,
operation: "kp#get-download-signed-url",
}
}
}

const signedUrl = getDownloadSignedUrlQuery.data;

if (!signedUrl || signedUrl.length === 0 || typeof signedUrl !== 'string') {
return {
success: false,
data: {
message: `An error occurred. Signed URL received from TRPC server is null: ${getDownloadSignedUrlQuery.error}`,
operation: "kp#get-download-signed-url",
}
}
}

return {
success: true,
data: {
type: "download-signed-url",
url: signedUrl
}
}
}

catch (error: unknown) {
const err = error as Error;
return {
success: false,
data: {
message: `An error occurred. Error: ${err.message}`,
operation: "kp#get-download-signed-url",
}
}
}
throw new Error("Method not implemented.");
// try {

// const getDownloadSignedUrlQuery = this.api.kernel.sourceData.getDownloadSignedUrl.useQuery({
// protocol: protocol,
// relativePath: relativePath,
// });

// if (getDownloadSignedUrlQuery.error) {
// return {
// success: false,
// data: {
// message: `An error occurred. TRPC server query failed: ${JSON.stringify(getDownloadSignedUrlQuery.error)}`,
// operation: "kp#get-download-signed-url",
// }
// }
// }

// const signedUrl = getDownloadSignedUrlQuery.data;

// if (!signedUrl || signedUrl.length === 0 || typeof signedUrl !== 'string') {
// return {
// success: false,
// data: {
// message: `An error occurred. Signed URL received from TRPC server is null: ${getDownloadSignedUrlQuery.error}`,
// operation: "kp#get-download-signed-url",
// }
// }
// }

// return {
// success: true,
// data: {
// type: "download-signed-url",
// url: signedUrl
// }
// }
// }

// catch (error: unknown) {
// const err = error as Error;
// return {
// success: false,
// data: {
// message: `An error occurred. Error: ${err.message}`,
// operation: "kp#get-download-signed-url",
// }
// }
// }

}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/infrastructure/client/trpc/vanilla-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ const client = createTRPCClient<AppRouter>({
],
});

export type TVanillaAPI = typeof client;

export const api = client;
Loading

0 comments on commit 03f039f

Please sign in to comment.