Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed expo/examples/with-firebase-storage-upload incorrect asset reference #522

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions with-firebase-storage-upload/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ImagePicker from "expo-image-picker";
import { manipulateAsync, FlipType, SaveFormat } from 'expo-image-manipulator';
import { getApps, initializeApp } from "firebase/app";
import { getStorage, ref, uploadBytes, getDownloadURL } from "firebase/storage";
import React from "react";
Expand All @@ -15,6 +16,7 @@ import {
} from "react-native";
import * as Clipboard from "expo-clipboard";
import uuid from "uuid";
import { Platform } from 'react-native'

const firebaseConfig = {
apiKey: "AIzaSyAlZruO2T_JNOWn4ysfX6AryR6Dzm_VVaA",
Expand All @@ -29,7 +31,7 @@ if (!getApps().length) {
initializeApp(firebaseConfig);
}

// Firebase sets some timeers for a long period, which will trigger some warnings. Let's turn that off for this example
// Firebase sets some timers for a long period, which will trigger some warnings. Let's turn that off for this example
LogBox.ignoreLogs([`Setting a timer for a long period`]);

export default class App extends React.Component {
Expand Down Expand Up @@ -174,23 +176,45 @@ export default class App extends React.Component {
};

_handleImagePicked = async (pickerResult) => {
if (pickerResult.canceled) {
console.log("Image upload cancelled by user");
return;
}

console.log('Received image to handle', pickerResult)

manipulatedImage = await this._manipulateImage(pickerResult.assets[0].uri);

try {
this.setState({ uploading: true });

if (!pickerResult.cancelled) {
const uploadUrl = await uploadImageAsync(pickerResult.uri);
const uploadUrl = await uploadImageAsync(manipulatedImage.uri);
this.setState({ image: uploadUrl });
}

} catch (e) {
console.log(e);
alert("Upload failed, sorry :(");
} finally {
this.setState({ uploading: false });
}
};

_manipulateImage = async (uri) => {
const manipulatedImageResult = await manipulateAsync(
uri,
[{ rotate: 90 }, { flip: FlipType.Vertical }],
{ compress: 0.5, format: SaveFormat.PNG}
);
console.log('Image manipulated', manipulatedImageResult);

return manipulatedImageResult;
};

}

async function uploadImageAsync(uri) {
console.log('Received uri to upload', uri)

// Why are we using XMLHttpRequest? See:
// https://github.com/expo/expo/issues/2402#issuecomment-443726662
const blob = await new Promise((resolve, reject) => {
Expand All @@ -206,9 +230,13 @@ async function uploadImageAsync(uri) {
xhr.open("GET", uri, true);
xhr.send(null);
});
console.log('Blob created', blob)

const fileRef = ref(getStorage(), uuid.v4());
console.log('File reference created', fileRef)

const result = await uploadBytes(fileRef, blob);
console.log('Filed uploaded', result)

// We're done with the blob, close and release it
blob.close();
Expand Down
3 changes: 2 additions & 1 deletion with-firebase-storage-upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"react-dom": "18.2.0",
"react-native": "0.74.5",
"react-native-web": "~0.19.6",
"uuid": "3.4.0"
"uuid": "3.4.0",
"expo-image-manipulator": "~12.0.5"
},
"devDependencies": {
"@babel/core": "^7.19.3"
Expand Down