Skip to content

Instantly share code, notes, and snippets.

@mul1sh
Last active May 11, 2020 04:34
Show Gist options
  • Save mul1sh/504f57afa64d7567bccf62bab677a49d to your computer and use it in GitHub Desktop.
Save mul1sh/504f57afa64d7567bccf62bab677a49d to your computer and use it in GitHub Desktop.
// @flow
import * as ImageManipulator from "expo-image-manipulator";
import Firebase from "./Firebase";
import { AsyncStorage } from 'react-native';
export type Picture = {
uri: string,
width: number,
height: number
};
const id = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
export default class ImageUpload {
static uid(): string {
return `${id()}${id()}-${id()}-${id()}-${id()}-${id()}${id()}${id()}`;
}
static async preview( { uri } : Picture): Promise<string> {
const result = await ImageManipulator.manipulateAsync(
uri,
[{ resize: { width: 5, height: 5 }}],
{ base64: true, format: "jpeg" }
);
return `data:image/jpeg;base64,${result.base64 || ""}`;
}
static async savePictureLocally(pic: Picture, postId: string) : Promise<boolean> {
let savedPicLocally = false;
if (pic && postId) {
try {
await AsyncStorage.setItem(postId, JSON.stringify(pic));
savedPicLocally = true;
} catch (e) {
// saving error
console.log("unable to save local pic", e);
}
}
return savedPicLocally;
}
static async deletePictureLocally(postId: string) : Promise<boolean> {
let deletedPicLocally = false;
if (postId) {
try {
await AsyncStorage.removeItem(postId);
deletedPicLocally = true;
} catch (e) {
// delete error
console.log("unable to delete local pic", e);
}
}
return deletedPicLocally;
}
static async getLocalPic(postId: string) : Promise<Picture> {
let pic: Picture = null;
if (postId) {
try {
const picString = await AsyncStorage.getItem(postId);
pic = JSON.parse(picString);
} catch (e) {
// get error
console.log("unable to get local pic", e);
}
}
return pic;
}
static async upload(ogPic: Picture, postId: string): Promise<string> {
const picture = await ImageManipulator.manipulateAsync(ogPic.uri,
[{ compress: 0.5, resize: { width: 500, height: 500 * (ogPic.height / ogPic.width) }}],
{ base64: true, format: "jpeg" }
);
if (postId) {
await ImageUpload.savePictureLocally(picture, postId);
}
const blob = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
// eslint-disable-next-line
xhr.onload = function () {
resolve(xhr.response);
};
// eslint-disable-next-line
xhr.onerror = function (e) {
// eslint-disable-next-line
console.log(e);
reject(new TypeError("Network request failed"));
};
xhr.responseType = "blob";
xhr.open("GET", picture.uri, true);
xhr.send(null);
});
const naseApp = Firebase.storage;
// console.log('Firebase.storage',Firebase.storage)
naseApp.setMaxUploadRetryTime(770000000);
naseApp.setMaxOperationRetryTime(770000000);
// console.log('maxUploadRetryTime', Firebase.storage.maxUploadRetryTime)
// console.log('xxx',naseApp.maxUploadRetryTime)
// console.log('Firebase..FirebaseStorage',Firebase.storage.app.FirebaseStorage)
// await Firebase.storage.app.setMaxUploadRetryTimeMillis(700000)
const ref = Firebase
.storage
.ref()
.child(ImageUpload.uid());
// await ref.setMaxUploadRetryTimeMillis(55120000)
const snapshot = await ref.put(blob);
const downloadURL = await snapshot.ref.getDownloadURL();
return downloadURL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment