Fakultas Ilmu Komputer UI

Commit 4c0f0546 authored by Ahmad Izzudin Alifyandra's avatar Ahmad Izzudin Alifyandra
Browse files

[FEATURE] Add image upload helper functions

parent 463790cd
// TODO add tests for image functions
import * as ImagePicker from "expo-image-picker";
import { Alert } from "react-native";
import firebase from "firebase/app";
import "firebase/storage";
export const pickImage =
async (): Promise<ImagePicker.ImagePickerResult | null> => {
const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
if (status !== "granted") {
Alert.alert("Unable to grant access to gallery");
return null;
} else {
const pickerResult = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [1, 1],
});
return pickerResult;
}
};
export const uploadImgToFirebase = async (
uri: string,
path: string,
setLoading?: React.Dispatch<React.SetStateAction<boolean>>
): Promise<string> => {
try {
setLoading && setLoading(true);
const blob: Blob = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = function () {
resolve(xhr.response);
};
xhr.onerror = function (e) {
console.log(e);
reject(new TypeError("Network request failed"));
};
xhr.responseType = "blob";
xhr.open("GET", uri, true);
xhr.send(null);
});
const ref = firebase.storage().ref().child(path);
const snapshot = await ref.put(blob);
// We're done with the blob, close and release it
// blob.close();
return (await snapshot.ref.getDownloadURL()) as string;
} catch (e) {
console.log(e);
Alert.alert("Upload failed");
} finally {
setLoading && setLoading(false);
}
};
......@@ -4997,6 +4997,15 @@ expo-font@~9.2.1:
expo-modules-core "~0.2.0"
fontfaceobserver "^2.1.0"
expo-image-picker@^10.2.3:
version "10.2.3"
resolved "https://registry.yarnpkg.com/expo-image-picker/-/expo-image-picker-10.2.3.tgz#204c83ba0731f2ccdbc42e13f9bf6e37be21c354"
integrity sha512-8VXLYjclXoQJHbdNLI21rdbnxFisBpZ6TgIifHf9kZ/momFBegUNqEKCjosvxVGVM8f7qaQxJV/znNtW0rDM/w==
dependencies:
"@expo/config-plugins" "^3.0.0"
expo-modules-core "~0.2.0"
uuid "7.0.2"
expo-keep-awake@~9.2.0:
version "9.2.0"
resolved "https://registry.yarnpkg.com/expo-keep-awake/-/expo-keep-awake-9.2.0.tgz"
......@@ -10633,6 +10642,11 @@ utils-merge@1.0.1:
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz"
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
uuid@7.0.2:
version "7.0.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-7.0.2.tgz#7ff5c203467e91f5e0d85cfcbaaf7d2ebbca9be6"
integrity sha512-vy9V/+pKG+5ZTYKf+VcphF5Oc6EFiu3W8Nv3P3zIh0EqVI80ZxOzuPfe9EHjkFNvf8+xuTHVeei4Drydlx4zjw==
uuid@^3.3.2, uuid@^3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment