feat: Enhance document management with a first draft of upload and download functionalities (needs revision)
This commit is contained in:
149
utils/documentUtils.tsx
Normal file
149
utils/documentUtils.tsx
Normal file
@ -0,0 +1,149 @@
|
||||
import api from '@/utils/api';
|
||||
import { Directory, File, Paths } from 'expo-file-system';
|
||||
import * as FileSystem from 'expo-file-system/legacy';
|
||||
import { StorageAccessFramework } from 'expo-file-system/legacy';
|
||||
import * as Sharing from 'expo-sharing';
|
||||
import * as Linking from 'expo-linking';
|
||||
import { Platform } from 'react-native';
|
||||
|
||||
/**
|
||||
* Gestisce l'upload di un documento verso il server usando Expo FileSystem
|
||||
* @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)
|
||||
*/
|
||||
export const uploadDocument = async (
|
||||
file: any,
|
||||
siteId: number | null,
|
||||
customTitle?: string
|
||||
): Promise<void> => {
|
||||
if (!file || !file.uri) {
|
||||
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);
|
||||
}
|
||||
|
||||
// TODO: Funzione di upload (manca lato backend)
|
||||
};
|
||||
|
||||
/**
|
||||
* Scarica un documento e offre di aprirlo/condividerlo (expo-sharing)
|
||||
* @param attachmentId ID o URL relativo del documento
|
||||
* @param fileName Nome con cui salvare il file
|
||||
* @param fileUrl URL completo del file da scaricare
|
||||
*/
|
||||
export const downloadAndShareDocument = async (
|
||||
mimetype: string,
|
||||
fileName: string,
|
||||
fileUrl: string
|
||||
): Promise<void> => {
|
||||
try {
|
||||
// TODO: Gestire meglio il download (attualmente si basa su expo-sharing)
|
||||
if (!fileUrl || !fileName) {
|
||||
throw new Error("Parametri mancanti per il download del documento.");
|
||||
}
|
||||
|
||||
const destination = new Directory(Paths.cache, 'documents');
|
||||
destination.exists ? destination.delete() : null;
|
||||
destination.create({ overwrite: true });
|
||||
|
||||
const tmpFile = await File.downloadFileAsync(fileUrl, destination);
|
||||
console.log("File temporaneo scaricato in:", tmpFile.uri);
|
||||
|
||||
const outFile = new File(destination, fileName);
|
||||
await tmpFile.move(outFile);
|
||||
console.log("File spostato in:", outFile.uri);
|
||||
console.log("File type:", mimetype);
|
||||
|
||||
if (await Sharing.isAvailableAsync()) {
|
||||
await Sharing.shareAsync(outFile.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 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