API upload image
Add: - utils/pickImage.ts - service/upload-service.ts - constants/directory-id.ts - constants/base-url-api-strorage.ts ### No Issue
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import axios, { AxiosInstance } from "axios";
|
||||
import Constants from "expo-constants";
|
||||
const API_BASE_URL = Constants.expoConfig?.extra?.API_BASE_URL;
|
||||
export const API_BASE_URL = Constants.expoConfig?.extra?.API_BASE_URL;
|
||||
|
||||
export const apiConfig: AxiosInstance = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
|
||||
78
service/upload-service.ts
Normal file
78
service/upload-service.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import { API_BASE_URL, apiConfig } from "@/service/api-config";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import axios from "axios";
|
||||
import Toast from "react-native-toast-message";
|
||||
|
||||
export async function uploadImageService({
|
||||
dirId,
|
||||
imageUri,
|
||||
}: {
|
||||
dirId: string;
|
||||
imageUri: string | null;
|
||||
}) {
|
||||
const token = await AsyncStorage.getItem("authToken");
|
||||
const url = `${API_BASE_URL}/mobile/upload`;
|
||||
|
||||
console.log("url >>", url);
|
||||
|
||||
if (!imageUri) {
|
||||
Toast.show({
|
||||
type: "error",
|
||||
text1: "Gagal",
|
||||
text2: "Harap pilih gambar terlebih dahulu",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const uri = imageUri;
|
||||
const filename = uri.split("/").pop();
|
||||
const match = /\.(\w+)$/.exec(filename || "");
|
||||
const type = match ? `image/${match[1]}` : "image";
|
||||
|
||||
const formData = new FormData();
|
||||
// @ts-ignore: React Native tidak mengenal Blob secara langsung
|
||||
formData.append("file", {
|
||||
uri,
|
||||
name: filename,
|
||||
type,
|
||||
});
|
||||
formData.append("dirId", dirId);
|
||||
|
||||
console.log("Form data >>", JSON.stringify(formData, null, 2));
|
||||
|
||||
const response = await axios.post(url, formData, {
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
timeout: 30000,
|
||||
});
|
||||
|
||||
console.log("Response", JSON.stringify(response, null, 2));
|
||||
const { data } = response;
|
||||
|
||||
if (!data.success) {
|
||||
Toast.show({
|
||||
type: "error",
|
||||
text1: "Gagal",
|
||||
text2: data.message,
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Toast.show({
|
||||
type: "success",
|
||||
text1: "File berhasil diunggah",
|
||||
});
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
Toast.show({
|
||||
type: "error",
|
||||
text1: "File gagal diunggah",
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user