feat: Add document download and upload. Add NFC support and enhance attendance and permits views
- Improved error message handling in LoginScreen for invalid credentials. - Added new images: mariani-icon.png and mariani-splash.png. - Updated AddDocumentModal to handle file extensions and improve UI. - Enhanced CalendarWidget to support month change callbacks. - Introduced NfcScanModal for NFC tag scanning with animations. - Revamped QrScanModal to utilize camera for QR code scanning. - Removed mock data from data.ts and streamlined Office data. - Updated package dependencies for expo-camera and react-native-nfc-manager. - Added utility function to parse seconds to time format. - Refactored document upload logic to use FormData for server uploads.
This commit is contained in:
@ -7,7 +7,7 @@ import * as Linking from 'expo-linking';
|
||||
import { Platform } from 'react-native';
|
||||
|
||||
/**
|
||||
* Gestisce l'upload di un documento verso il server usando Expo FileSystem
|
||||
* Gestisce l'upload di un documento verso il server usando FormData
|
||||
* @param file File da caricare (deve avere almeno la proprietà 'uri')
|
||||
* @param siteId ID del sito a cui associare il documento (null per registro generale)
|
||||
* @param customTitle Titolo personalizzato per il documento (opzionale)
|
||||
@ -21,13 +21,46 @@ export const uploadDocument = async (
|
||||
throw new Error("File non valido per l'upload.");
|
||||
}
|
||||
|
||||
if (siteId === null) {
|
||||
console.log("Uploading document:", file, "to registry with title:", customTitle);
|
||||
} else {
|
||||
console.log("Uploading document:", file, "to site:", siteId, "with title:", customTitle);
|
||||
}
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('file', {
|
||||
uri: file.uri,
|
||||
name: customTitle || file.name,
|
||||
type: file.mimeType
|
||||
} as any);
|
||||
|
||||
// TODO: Funzione di upload (manca lato backend)
|
||||
if (siteId !== null) {
|
||||
formData.append('siteId', siteId.toString());
|
||||
}
|
||||
|
||||
if (customTitle) {
|
||||
formData.append('customTitle', customTitle.trim());
|
||||
}
|
||||
|
||||
const response = await api.post('/attachment/upload', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
}
|
||||
});
|
||||
|
||||
console.log("Risposta server:", response.data);
|
||||
|
||||
if (response.data?.status === 'error') {
|
||||
throw new Error(response.data.message || "Errore sconosciuto dal server");
|
||||
}
|
||||
|
||||
} catch (error: any) {
|
||||
console.error("Errore durante l'upload del documento:", error);
|
||||
|
||||
if (error.response) {
|
||||
const serverMessage = error.response.data?.message || error.message;
|
||||
throw new Error(`Errore Server (${error.response.status}): ${serverMessage}`);
|
||||
} else if (error.request) {
|
||||
throw new Error("Il server non risponde. Controlla la connessione.");
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@ -74,76 +107,3 @@ export const downloadAndShareDocument = async (
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: Test con versione legacy di FileSystem e SAF
|
||||
export const downloadDocumentLegacy = async (
|
||||
mimetype: string,
|
||||
fileName: string,
|
||||
fileUrl: string
|
||||
): Promise<void> => {
|
||||
try {
|
||||
if (!fileUrl || !fileName) {
|
||||
throw new Error("Parametri mancanti per il download del documento.");
|
||||
}
|
||||
|
||||
const path = FileSystem.cacheDirectory + 'documents/';
|
||||
// Download del file nella directory selezionata
|
||||
const tmpFile = await FileSystem.downloadAsync(fileUrl, path + fileName);
|
||||
console.log("File temporaneo scaricato in:", tmpFile.uri);
|
||||
|
||||
if (Platform.OS === 'android') {
|
||||
const permission = await StorageAccessFramework.requestDirectoryPermissionsAsync();
|
||||
if (permission.granted) {
|
||||
// Gets SAF URI from response
|
||||
const safUri = permission.directoryUri;
|
||||
console.log("Selected Directory URI:", safUri);
|
||||
|
||||
const fileContent = await FileSystem.readAsStringAsync(tmpFile.uri, { encoding: FileSystem.EncodingType.Base64 });
|
||||
console.log("File letto in Base64, dimensione:", fileContent.length);
|
||||
|
||||
// Copia il file nella directory SAF selezionata
|
||||
const destUri = await StorageAccessFramework.createFileAsync(
|
||||
safUri,
|
||||
fileName,
|
||||
mimetype
|
||||
);
|
||||
console.log("Destinazione SAF URI:", destUri);
|
||||
|
||||
await FileSystem.writeAsStringAsync(destUri, fileContent, { encoding: FileSystem.EncodingType.Base64 });
|
||||
console.log("File riscritto in SAF.");
|
||||
}
|
||||
} else if (Platform.OS === 'ios') {
|
||||
if (await Sharing.isAvailableAsync()) {
|
||||
await Sharing.shareAsync(tmpFile.uri, {
|
||||
mimeType: mimetype,
|
||||
dialogTitle: `Scarica ${fileName}`,
|
||||
UTI: 'public.item'
|
||||
});
|
||||
} else {
|
||||
throw new Error("Condivisione non supportata su questo dispositivo.");
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error("Download Error:", error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: Test con Linking standard
|
||||
export const downloadDocumentByUrl = async (
|
||||
fileUrl: string,
|
||||
fileName: string
|
||||
): Promise<void> => {
|
||||
try {
|
||||
if (!fileUrl || !fileName) {
|
||||
throw new Error("Parametri mancanti per il download del documento.");
|
||||
}
|
||||
|
||||
Linking.openURL(fileUrl);
|
||||
|
||||
} catch (error) {
|
||||
console.error("Download Error:", error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user