Skip to content

Commit

Permalink
Merge pull request #52 from openhacku-team-a/feture/upload-img-method
Browse files Browse the repository at this point in the history
[Add] image uploader
  • Loading branch information
Najah7 authored Mar 15, 2024
2 parents 86c23d6 + 2b84d01 commit a67ce40
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
39 changes: 38 additions & 1 deletion src/hooks/useApiPBClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,44 @@ const useApiPBClient = () => {
return { data: null, unauthorized: false, error: resp.statusText };
});
};
return { get, put, post };

const imageUpload = async (
userId: string,
file: File,
): Promise<BackendResponse> => {
const contentType = getFileContentType(file);

headers["Content-Type"] = contentType;

return await axios
.put(`/user/${userId}/icon`, file, { headers })
.then((resp) => {
if (resp.status < 210) {
return { data: resp.data, unauthorized: false, error: null };
}
if (resp.status === 401) {
return { data: null, unauthorized: true, error: resp.status };
}
return { data: null, unauthorized: false, error: resp.statusText };
});
};
return { get, put, post, imageUpload };
};

export default useApiPBClient;

function getFileContentType(file: File): string {
const extension = file.name.split(".").pop();
switch (extension) {
case "png":
return "image/png";
case "jpg":
case "jpeg":
return "image/jpeg";
case "gif":
return "image/gif";
// 追加の拡張子に対する処理を追加できます
default:
return "application/octet-stream"; // デフォルトのContent-Type
}
}
16 changes: 3 additions & 13 deletions src/hooks/useFetchUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,9 @@ const useFetchUser = () => {
});
};

// const icon = async (userId: string, file: File) => {
// return await client.put(
// `/user/${userId}/icon`,
// ).then((resp) => {
// if (resp.unauthorized) {
// throw new Error("unauthorized");
// }
// if (resp.error) {
// throw new Error(resp.error);
// }
// return UserInfoResponse.fromBinary(resp.data)
// })
// }
const iconUpload = async (userId: string, file: File) => {
return await client.imageUpload(userId, file);
};

return { me, get, list, create };
};
Expand Down

0 comments on commit a67ce40

Please sign in to comment.