mirror of
https://github.com/chinchang/web-maker.git
synced 2025-07-23 23:11:12 +02:00
migrate storage to firebase 10
This commit is contained in:
@@ -1,6 +1,12 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import firebase from 'firebase/app';
|
import {
|
||||||
import 'firebase/storage';
|
deleteObject,
|
||||||
|
uploadBytesResumable,
|
||||||
|
ref,
|
||||||
|
listAll,
|
||||||
|
getDownloadURL
|
||||||
|
} from 'firebase/storage';
|
||||||
|
import { storage } from '../firebaseInit';
|
||||||
import { HStack, Stack, VStack } from './Stack';
|
import { HStack, Stack, VStack } from './Stack';
|
||||||
import { copyToClipboard } from '../utils';
|
import { copyToClipboard } from '../utils';
|
||||||
import { Trans } from '@lingui/macro';
|
import { Trans } from '@lingui/macro';
|
||||||
@@ -17,6 +23,7 @@ function getFileType(url) {
|
|||||||
}
|
}
|
||||||
return ext;
|
return ext;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Assets = ({ onProBtnClick, onLoginBtnClick }) => {
|
const Assets = ({ onProBtnClick, onLoginBtnClick }) => {
|
||||||
const [files, setFiles] = useState([]);
|
const [files, setFiles] = useState([]);
|
||||||
const [isFetchingFiles, setIsFetchingFiles] = useState(false);
|
const [isFetchingFiles, setIsFetchingFiles] = useState(false);
|
||||||
@@ -26,8 +33,6 @@ const Assets = ({ onProBtnClick, onLoginBtnClick }) => {
|
|||||||
const [uploadProgress, setUploadProgress] = useState();
|
const [uploadProgress, setUploadProgress] = useState();
|
||||||
const [listType, setListType] = useState('grid');
|
const [listType, setListType] = useState('grid');
|
||||||
|
|
||||||
const storageRef = firebase.storage().ref(`assets/${window.user?.uid}`);
|
|
||||||
|
|
||||||
const uploadFile = file => {
|
const uploadFile = file => {
|
||||||
if (file.size > 1024 * 1024) {
|
if (file.size > 1024 * 1024) {
|
||||||
// 1MB limit
|
// 1MB limit
|
||||||
@@ -39,9 +44,11 @@ const Assets = ({ onProBtnClick, onLoginBtnClick }) => {
|
|||||||
const metadata = {
|
const metadata = {
|
||||||
cacheControl: 'public, max-age=3600' // 1 hr
|
cacheControl: 'public, max-age=3600' // 1 hr
|
||||||
};
|
};
|
||||||
|
const task = uploadBytesResumable(
|
||||||
const fileRef = storageRef.child(file.name);
|
ref(storage, `assets/${window.user?.uid}/${file.name}`),
|
||||||
const task = fileRef.put(file, metadata);
|
file,
|
||||||
|
metadata
|
||||||
|
);
|
||||||
|
|
||||||
task.on(
|
task.on(
|
||||||
'state_changed',
|
'state_changed',
|
||||||
@@ -49,12 +56,12 @@ const Assets = ({ onProBtnClick, onLoginBtnClick }) => {
|
|||||||
// Observe state change events such as progress, pause, and resume
|
// Observe state change events such as progress, pause, and resume
|
||||||
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
|
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
|
||||||
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
|
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
|
||||||
console.log('Upload is ' + progress + '% done');
|
// console.log('Upload is ' + progress + '% done');
|
||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
// Handle unsuccessful uploads
|
// Handle unsuccessful uploads
|
||||||
setIsUploading(false);
|
setIsUploading(false);
|
||||||
console.error('File upload error:', error);
|
// console.log('File upload error:', error);
|
||||||
alertsService.add('⚠️ File upload failed');
|
alertsService.add('⚠️ File upload failed');
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
@@ -78,11 +85,11 @@ const Assets = ({ onProBtnClick, onLoginBtnClick }) => {
|
|||||||
// Function to fetch existing files
|
// Function to fetch existing files
|
||||||
const fetchFiles = () => {
|
const fetchFiles = () => {
|
||||||
setIsFetchingFiles(true);
|
setIsFetchingFiles(true);
|
||||||
storageRef
|
|
||||||
.listAll()
|
listAll(ref(storage, `assets/${window.user?.uid}`))
|
||||||
.then(result => {
|
.then(result => {
|
||||||
const filePromises = result.items.map(item => {
|
const filePromises = result.items.map(item => {
|
||||||
return item.getDownloadURL().then(url => {
|
return getDownloadURL(item).then(url => {
|
||||||
return { name: item.name, url };
|
return { name: item.name, url };
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -190,9 +197,8 @@ const Assets = ({ onProBtnClick, onLoginBtnClick }) => {
|
|||||||
const file = files[index];
|
const file = files[index];
|
||||||
const answer = confirm(`Are you sure you want to delete "${file.name}"?`);
|
const answer = confirm(`Are you sure you want to delete "${file.name}"?`);
|
||||||
if (!answer) return;
|
if (!answer) return;
|
||||||
const fileRef = storageRef.child(file.name);
|
const fileRef = ref(storage, file.url);
|
||||||
fileRef
|
deleteObject(fileRef)
|
||||||
.delete()
|
|
||||||
.then(() => {
|
.then(() => {
|
||||||
alertsService.add('File deleted successfully');
|
alertsService.add('File deleted successfully');
|
||||||
setFiles(files.filter((_, i) => i !== index));
|
setFiles(files.filter((_, i) => i !== index));
|
||||||
@@ -259,6 +265,15 @@ const Assets = ({ onProBtnClick, onLoginBtnClick }) => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{isFetchingFiles && <LoaderWithText>Fetching files...</LoaderWithText>}
|
{isFetchingFiles && <LoaderWithText>Fetching files...</LoaderWithText>}
|
||||||
|
|
||||||
|
{!isFetchingFiles && !files.length ? (
|
||||||
|
<Stack justify="center">
|
||||||
|
<Text align="center" appearance="secondary">
|
||||||
|
No files uploaded yet
|
||||||
|
</Text>
|
||||||
|
</Stack>
|
||||||
|
) : null}
|
||||||
|
|
||||||
<VStack align="stretch" gap={1}>
|
<VStack align="stretch" gap={1}>
|
||||||
{files.length ? (
|
{files.length ? (
|
||||||
<Stack gap={1}>
|
<Stack gap={1}>
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import { initializeApp } from 'firebase/app';
|
import { initializeApp } from 'firebase/app';
|
||||||
import { getAuth } from 'firebase/auth';
|
import { getAuth } from 'firebase/auth';
|
||||||
import { getFirestore } from 'firebase/firestore';
|
import { getFirestore } from 'firebase/firestore';
|
||||||
|
import { getStorage } from 'firebase/storage';
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
apiKey: 'AIzaSyBl8Dz7ZOE7aP75mipYl2zKdLSRzBU2fFc',
|
apiKey: 'AIzaSyBl8Dz7ZOE7aP75mipYl2zKdLSRzBU2fFc',
|
||||||
@@ -16,3 +17,4 @@ export { app };
|
|||||||
|
|
||||||
export const auth = getAuth(app);
|
export const auth = getAuth(app);
|
||||||
export const db = getFirestore(app);
|
export const db = getFirestore(app);
|
||||||
|
export const storage = getStorage(app);
|
||||||
|
Reference in New Issue
Block a user