112 lines
3.6 KiB
TypeScript
112 lines
3.6 KiB
TypeScript
import api from '@/utils/api';
|
|
import { Directory, File, Paths } from 'expo-file-system';
|
|
import * as Sharing from 'expo-sharing';
|
|
import { Platform } from 'react-native';
|
|
|
|
interface UploadOptions {
|
|
endpoint: string;
|
|
fileKey?: string;
|
|
extraData?: Record<string, string>;
|
|
}
|
|
|
|
/**
|
|
* Handles upload of a document through the server using FormData
|
|
* @param file File to upload (must have at least the 'uri' property)
|
|
* @param options Configuration for the upload
|
|
*/
|
|
export const uploadDocument = async (
|
|
file: any,
|
|
options: UploadOptions
|
|
): Promise<any> => {
|
|
if (!file || !file.uri) {
|
|
throw new Error("File non valido per l'upload.");
|
|
}
|
|
|
|
try {
|
|
const formData = new FormData();
|
|
const fileKey = options.fileKey || 'file';
|
|
|
|
formData.append(fileKey, {
|
|
uri: Platform.OS === 'android' ? file.uri : file.uri.replace('file://', ''),
|
|
name: file.name,
|
|
type: file.mimeType || 'application/octet-stream'
|
|
} as any);
|
|
|
|
if (options.extraData) {
|
|
Object.keys(options.extraData).forEach(key => {
|
|
formData.append(key, options.extraData![key]);
|
|
});
|
|
}
|
|
|
|
const response = await api.post(options.endpoint, formData, {
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data',
|
|
}
|
|
});
|
|
|
|
console.log("Risposta server upload:", response.data);
|
|
|
|
if (response.data?.status === 'error' || response.data?.success === false) {
|
|
throw new Error(response.data.message || "Errore sconosciuto dal server");
|
|
}
|
|
|
|
return response.data;
|
|
} 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;
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Download and share a document (expo-sharing)
|
|
* @param attachmentId ID or relative URL of the document
|
|
* @param fileName Name to save the file as
|
|
* @param fileUrl Full URL of the file to download
|
|
*/
|
|
export const downloadAndShareDocument = async (
|
|
mimetype: string,
|
|
fileName: string,
|
|
fileUrl: string
|
|
): Promise<void> => {
|
|
try {
|
|
// TODO: Download based on expo-sharing - some mime types may not be supported
|
|
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;
|
|
}
|
|
};
|