File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
import React from 'react';
import {Linking} from 'react-native';
import {Camera} from 'react-native-vision-camera';
// import RNFS from 'react-native-fs';
import {getTokenFromStorage} from './loginUtils';
// API 주소
const url = 'http://takensoftai.iptime.org:15857/action/image_summit';
type PermissionCallback = (result: boolean) => void;
type GpsPermissionCallback = (result: boolean) => void;
interface UseCameraApi {
requestStoragePermission: (callback: PermissionCallback) => Promise<void>;
isPermission: boolean;
requestGPSPermission: (callback: GpsPermissionCallback) => Promise<void>;
isGpsPermission: boolean;
sendPhotoFile: (photoPath: string) => Promise<void>;
}
// 권한 요청
export default function useCameraApi(): UseCameraApi {
const [isPermission, setIsPermission] = React.useState<boolean>(false);
const [isGpsPermission, setIsGpsPermission] = React.useState<boolean>(false);
// 카메라 권한 요청
const requestStoragePermission = async (callback: PermissionCallback) => {
try {
const cameraPermission = await Camera.getCameraPermissionStatus();
let newCameraPermission = cameraPermission;
if (
cameraPermission === 'not-determined' ||
cameraPermission === 'denied'
) {
newCameraPermission = await Camera.requestCameraPermission();
}
const permissionGranted = newCameraPermission === 'granted';
setIsPermission(permissionGranted);
callback(permissionGranted);
if (!permissionGranted) {
await Linking.openSettings();
}
} catch (err) {
console.log(`카메라 권한 요청 에러 : ${err}`);
}
};
// 위치 권한 요청
const requestGPSPermission = async (callback: GpsPermissionCallback) => {
try {
const gpsPermissionStatus = await Camera.getLocationPermissionStatus();
let newGpsPermission = gpsPermissionStatus;
if (
newGpsPermission === 'not-determined' ||
newGpsPermission === 'denied' ||
newGpsPermission === 'restricted'
) {
newGpsPermission = await Camera.requestLocationPermission();
}
const gpsPermissionGranted = newGpsPermission === 'granted';
setIsGpsPermission(gpsPermissionGranted);
callback(gpsPermissionGranted);
if (!gpsPermissionGranted) {
await Linking.openSettings();
}
} catch (err) {
console.log(`위치 권한 요청 에러 : ${err}`);
}
};
// 사진 전송
const sendPhotoFile = async (photoPath: string) => {
try {
const token = await getTokenFromStorage();
if (!token) {
console.log('토큰 없음');
return;
}
if (!photoPath || typeof photoPath !== 'string') {
console.log('사진 경로 없음');
return;
}
const pathSplit = photoPath.split('/').pop();
if (!pathSplit) {
console.log('사진 경로 split 불가');
return;
}
const fileName = pathSplit.split('.')[0];
const formData = new FormData();
formData.append('file', {
uri: `file://${photoPath}`,
name: `${fileName}.jpg`,
type: 'image/jpg',
});
try {
const response = await fetch(url, {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'multipart/form-data',
Authorization: `${token}`,
},
});
const responseJson = await response.json();
if (response.status === 200) {
console.log(
'Upload Successful',
`(${response.status})`,
responseJson,
);
} else {
console.log('Upload failed', responseJson);
}
} catch (error) {
console.log('Upload error', error);
}
} catch (error) {}
};
return {
requestStoragePermission,
isPermission,
requestGPSPermission,
isGpsPermission,
sendPhotoFile,
};
}