| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import AsyncStorage from '@react-native-async-storage/async-storage'; |
|
|
| |
| const GCS_CONFIG = { |
| bucketName: 'newimagesndma', |
| projectId: 'axiomatic-skill-473605-i3', |
| |
| |
| getPublicUrl: (fileName) => `https://storage.googleapis.com/newimagesndma/${fileName}`, |
| |
| |
| uploadEndpoint: (fileName) => |
| `https://storage.googleapis.com/upload/storage/v1/b/newimagesndma/o?uploadType=media&name=${encodeURIComponent(fileName)}`, |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export const uploadToGCS = async (fileUri, folder = 'uploads', fileName = null, contentType = 'image/jpeg') => { |
| try { |
| |
| if (!fileName) { |
| const timestamp = Date.now(); |
| const extension = fileUri.split('.').pop() || 'jpg'; |
| const randomId = Math.random().toString(36).substr(2, 9); |
| fileName = `${folder}/${timestamp}_${randomId}.${extension}`; |
| } else { |
| fileName = `${folder}/${fileName}`; |
| } |
| |
| console.log(`📤 Uploading to GCS: ${fileName}`); |
| |
| |
| const response = await fetch(fileUri); |
| const blob = await response.blob(); |
| |
| |
| const uploadUrl = GCS_CONFIG.uploadEndpoint(fileName); |
| console.log('🔗 Upload URL:', uploadUrl); |
| |
| const uploadResponse = await fetch(uploadUrl, { |
| method: 'POST', |
| headers: { |
| 'Content-Type': contentType, |
| }, |
| body: blob, |
| }); |
|
|
| if (!uploadResponse.ok) { |
| const errorText = await uploadResponse.text(); |
| console.error('❌ GCS Upload failed:', uploadResponse.status, errorText); |
| throw new Error(`Upload failed: ${uploadResponse.status}`); |
| } |
|
|
| const publicUrl = GCS_CONFIG.getPublicUrl(fileName); |
| console.log('✅ File uploaded to GCS:', publicUrl); |
| |
| |
| await AsyncStorage.setItem(`@file_${fileName}`, publicUrl); |
| |
| return publicUrl; |
| } catch (error) { |
| console.error('❌ GCS upload error:', error); |
| throw error; |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| export const uploadProfilePicture = async (imageUri, userId) => { |
| const safeUserId = userId.replace(/[@.]/g, '_'); |
| const fileName = `${safeUserId}_${Date.now()}.jpg`; |
| const publicUrl = await uploadToGCS(imageUri, 'profiles', fileName, 'image/jpeg'); |
| |
| |
| await AsyncStorage.setItem(`@profile_pic_${userId}`, publicUrl); |
| |
| return publicUrl; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| export const uploadCSV = async (fileUri, fileName) => { |
| return await uploadToGCS(fileUri, 'csv', fileName, 'text/csv'); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| export const uploadImage = async (imageUri, folder = 'images') => { |
| const timestamp = Date.now(); |
| const fileName = `img_${timestamp}.jpg`; |
| return await uploadToGCS(imageUri, folder, fileName, 'image/jpeg'); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| export const uploadTrainingImage = async (imageUri, reportId) => { |
| const fileName = `training_${reportId}_${Date.now()}.jpg`; |
| return await uploadToGCS(imageUri, 'training-reports', fileName, 'image/jpeg'); |
| }; |
|
|
| |
| |
| |
| |
| |
| export const getProfilePicture = async (userId) => { |
| try { |
| const url = await AsyncStorage.getItem(`@profile_pic_${userId}`); |
| return url; |
| } catch (error) { |
| console.error('Error loading profile picture:', error); |
| return null; |
| } |
| }; |
|
|
| |
| |
| |
| |
| export const deleteFromGCS = async (fileName) => { |
| console.warn('⚠️ Delete operation requires authentication. File will remain in bucket.'); |
| |
| try { |
| await AsyncStorage.removeItem(`@file_${fileName}`); |
| } catch (error) { |
| console.error('Error removing from storage:', error); |
| } |
| }; |
|
|
| |
| |
| |
| |
| export const listFiles = async (prefix = '') => { |
| try { |
| const keys = await AsyncStorage.getAllKeys(); |
| const fileKeys = keys.filter(key => key.startsWith('@file_')); |
| const files = []; |
| |
| for (const key of fileKeys) { |
| const url = await AsyncStorage.getItem(key); |
| if (url) { |
| files.push({ key, url }); |
| } |
| } |
| |
| return files; |
| } catch (error) { |
| console.error('Error listing files:', error); |
| return []; |
| } |
| }; |
|
|
| export default { |
| uploadToGCS, |
| uploadProfilePicture, |
| uploadCSV, |
| uploadImage, |
| uploadTrainingImage, |
| getProfilePicture, |
| deleteFromGCS, |
| listFiles, |
| GCS_CONFIG, |
| }; |
|
|