From 7c2587e9963284d083e237f317638cc0cd1ed960 Mon Sep 17 00:00:00 2001 From: Jarlath Holleran Date: Thu, 25 Jul 2024 15:45:02 +0100 Subject: [PATCH] WALLET-388: Retrieve resource and send to the Wallet Backend service. (#7) * Retrieve resource and send to the Wallet Backend service. No longer calling the collect endpoint --- api/files.ts | 41 ++++++++++++++++++++++++------------ app/(tabs)/home/download.tsx | 14 +++++++++--- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/api/files.ts b/api/files.ts index 0290fe0..df678a2 100644 --- a/api/files.ts +++ b/api/files.ts @@ -32,19 +32,39 @@ export const fetchFiles = async (): Promise => { }; export const postFile = async (file: FileObject): Promise => { - console.log(process.env.EXPO_PUBLIC_WALLET_API); - const formData = new FormData(); formData.append("file", { name: file.name, - type: mime.getType(file.name) || "application/octet-stream", + type: + file.contentType || mime.getType(file.name) || "application/octet-stream", uri: file.uri, } as unknown as Blob); - await fetch(`${process.env.EXPO_PUBLIC_WALLET_API}/wallet`, { - method: "PUT", - body: formData, - }); + try { + const response = await fetch( + `${process.env.EXPO_PUBLIC_WALLET_API}/wallet`, + { + method: "PUT", + body: formData, + } + ); + + if (response.ok) { + console.debug( + `Uploaded file to Wallet. HTTP response status:${response.status}` + ); + } else { + throw Error( + `Failed to upload file to Wallet. HTTP response status from Wallet Backend service:${ + response.status + }` + ); + } + } catch (error) { + throw Error("Failed to retrieve and upload file to Wallet", { + cause: error, + }); + } }; export const deleteFile = async (fileId: string): Promise => { @@ -57,10 +77,3 @@ export const getFile = async (fileId: string): Promise => { "GET" ); }; - -export const collectFileToPod = async (uri: string): Promise => { - return makeApiRequest( - `wallet/collect?url=${encodeURIComponent(uri)}`, - "PUT" - ); -}; diff --git a/app/(tabs)/home/download.tsx b/app/(tabs)/home/download.tsx index 3981bb3..3d89d26 100644 --- a/app/(tabs)/home/download.tsx +++ b/app/(tabs)/home/download.tsx @@ -24,7 +24,7 @@ import { useLocalSearchParams, useNavigation } from "expo-router"; import { ThemedText } from "@/components/ThemedText"; import CustomButton from "@/components/Button"; import { useMutation, useQueryClient } from "@tanstack/react-query"; -import { collectFileToPod } from "@/api/files"; +import { postFile } from "@/api/files"; import { FontAwesome6 } from "@expo/vector-icons"; import IconResourceName from "@/components/common/IconResourceName"; import { RDF_CONTENT_TYPE } from "@/utils/constants"; @@ -41,10 +41,14 @@ const Page: React.FC = () => { const parentNavigation = useNavigation("/(tabs)"); const mutation = useMutation({ - mutationFn: collectFileToPod, + mutationFn: postFile, onSuccess: async () => { await queryClient.invalidateQueries({ queryKey: ["files"] }); }, + onError: (error) => { + // TODO: there needs to be better error handling here... + console.warn(error); + }, mutationKey: ["filesMutation"], }); const { uri, contentType } = useLocalSearchParams(); @@ -56,7 +60,11 @@ const Page: React.FC = () => { const { goBack } = useNavigation(); const onSaveToWallet = async () => { - mutation.mutate(uri as string); + mutation.mutate({ + uri: uri as string, + name: fileName, + contentType, + }); goBack(); }; useLayoutEffect(() => {