|
| 1 | +package org.cryptomator.domain.usecases; |
| 2 | + |
| 3 | +import android.content.ContentResolver; |
| 4 | +import android.content.Context; |
| 5 | +import android.net.Uri; |
| 6 | +import android.provider.DocumentsContract; |
| 7 | + |
| 8 | +import org.cryptomator.domain.CloudFile; |
| 9 | +import org.cryptomator.domain.exception.BackendException; |
| 10 | +import org.cryptomator.domain.exception.FatalBackendException; |
| 11 | +import org.cryptomator.domain.exception.IllegalFileNameException; |
| 12 | +import org.cryptomator.domain.exception.NoSuchCloudFileException; |
| 13 | +import org.cryptomator.generator.Parameter; |
| 14 | +import org.cryptomator.generator.UseCase; |
| 15 | +import org.cryptomator.util.file.MimeType; |
| 16 | +import org.cryptomator.util.file.MimeTypes; |
| 17 | + |
| 18 | +import java.io.FileNotFoundException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +@UseCase |
| 23 | +public class PrepareDownloadFiles { |
| 24 | + |
| 25 | +private final Context context; |
| 26 | +private final MimeTypes mimeTypes; |
| 27 | +private final List<CloudFile> filesToExport; |
| 28 | +private final Uri parentUri; |
| 29 | +private final ContentResolver contentResolver; |
| 30 | +private final CloudNodeRecursiveListing cloudNodeRecursiveListing; |
| 31 | + |
| 32 | +private List<DownloadFile> downloadFiles = new ArrayList<>(); |
| 33 | + |
| 34 | +PrepareDownloadFiles(Context context, MimeTypes mimeTypes, @Parameter List<CloudFile> filesToExport, @Parameter Uri parentUri, @Parameter CloudNodeRecursiveListing cloudNodeRecursiveListing) { |
| 35 | +this.context = context; |
| 36 | +this.mimeTypes = mimeTypes; |
| 37 | +this.filesToExport = filesToExport; |
| 38 | +this.parentUri = parentUri; |
| 39 | +this.contentResolver = context.getContentResolver(); |
| 40 | +this.cloudNodeRecursiveListing = cloudNodeRecursiveListing; |
| 41 | +} |
| 42 | + |
| 43 | +List<DownloadFile> execute() throws BackendException, FileNotFoundException { |
| 44 | +downloadFiles = prepareFilesForExport(filesToExport, parentUri); |
| 45 | +for (CloudFolderRecursiveListing folderRecursiveListing : cloudNodeRecursiveListing.getFoldersContent()) { |
| 46 | +prepareFolderContentForExport(folderRecursiveListing, parentUri); |
| 47 | +} |
| 48 | +return downloadFiles; |
| 49 | +} |
| 50 | + |
| 51 | +private List<DownloadFile> prepareFilesForExport(List<CloudFile> filesToExport, Uri parentUri) throws NoSuchCloudFileException, FileNotFoundException, IllegalFileNameException { |
| 52 | +List<DownloadFile> downloadFiles = new ArrayList<>(); |
| 53 | +for (CloudFile cloudFile : filesToExport) { |
| 54 | +DownloadFile downloadFile = createDownloadFile(cloudFile, parentUri); |
| 55 | +downloadFiles.add(downloadFile); |
| 56 | +} |
| 57 | +return downloadFiles; |
| 58 | +} |
| 59 | + |
| 60 | +private void prepareFolderContentForExport(CloudFolderRecursiveListing folderRecursiveListing, Uri parentUri) throws FileNotFoundException, NoSuchCloudFileException, IllegalFileNameException { |
| 61 | +Uri createdFolder = createFolder(parentUri, folderRecursiveListing.getParent().getName()); |
| 62 | +if (createdFolder != null) { |
| 63 | +downloadFiles.addAll(prepareFilesForExport(folderRecursiveListing.getFiles(), createdFolder)); |
| 64 | +for (CloudFolderRecursiveListing childFolder : folderRecursiveListing.getFolders()) { |
| 65 | +prepareFolderContentForExport(childFolder, createdFolder); |
| 66 | +} |
| 67 | +} else { |
| 68 | +throw new FatalBackendException("Failed to create parent folder for export"); |
| 69 | +} |
| 70 | +} |
| 71 | + |
| 72 | +private Uri createFolder(Uri parentUri, String folderName) throws NoSuchCloudFileException { |
| 73 | +try { |
| 74 | +return DocumentsContract.createDocument(contentResolver, parentUri, DocumentsContract.Document.MIME_TYPE_DIR, folderName); |
| 75 | +} catch (FileNotFoundException e) { |
| 76 | +throw new NoSuchCloudFileException("Creating folder failed"); |
| 77 | +} |
| 78 | +} |
| 79 | + |
| 80 | +private DownloadFile createDownloadFile(CloudFile file, Uri documentUri) throws NoSuchCloudFileException, IllegalFileNameException { |
| 81 | +try { |
| 82 | +return new DownloadFile.Builder().setDownloadFile(file).setDataSink(contentResolver.openOutputStream(createNewDocumentUri(documentUri, file.getName()))).build(); |
| 83 | +} catch (FileNotFoundException e) { |
| 84 | +throw new NoSuchCloudFileException(file.getName()); |
| 85 | +} |
| 86 | +} |
| 87 | + |
| 88 | +private Uri createNewDocumentUri(Uri parentUri, String fileName) throws IllegalFileNameException, NoSuchCloudFileException { |
| 89 | +MimeType mimeType = mimeTypes.fromFilename(fileName); |
| 90 | +if (mimeType == null) { |
| 91 | +mimeType = MimeType.APPLICATION_OCTET_STREAM; |
| 92 | +} |
| 93 | +try { |
| 94 | +Uri documentUri = DocumentsContract.createDocument(context.getContentResolver(), parentUri, mimeType.toString(), fileName); |
| 95 | +if (documentUri == null) { |
| 96 | +throw new IllegalFileNameException(); |
| 97 | +} |
| 98 | +return documentUri; |
| 99 | +} catch (FileNotFoundException e) { |
| 100 | +throw new NoSuchCloudFileException(fileName); |
| 101 | +} |
| 102 | +} |
| 103 | + |
| 104 | +} |
0 commit comments