diff options
author | Stefan Niedermann <info@niedermann.it> | 2021-03-22 13:52:26 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2021-03-22 13:52:40 +0300 |
commit | 3e350e6648e0ad605da553f4642968642290e231 (patch) | |
tree | 7d61898113979ab9538749b2b344a2f1087603e3 /app/src/main/java/it/niedermann/nextcloud/deck/util | |
parent | 2abfe93975473ae84c905c10254808709832cd63 (diff) |
Share logs as file
Signed-off-by: Stefan Niedermann <info@niedermann.it>
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud/deck/util')
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java | 60 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/util/FilesUtil.java | 78 |
2 files changed, 78 insertions, 60 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java index 72151832e..a4bd5d6d9 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java @@ -11,13 +11,6 @@ import androidx.annotation.DrawableRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.Px; -import androidx.annotation.WorkerThread; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; import it.niedermann.nextcloud.deck.DeckLog; import it.niedermann.nextcloud.deck.R; @@ -99,59 +92,6 @@ public class AttachmentUtil { return accountUrl + "/index.php/apps/deck/cards/" + cardRemoteId + "/attachment/" + attachmentRemoteId; } - /** - * https://help.nextcloud.com/t/android-app-select-file-with-nextcloud-app-file-cant-be-read/103706 - * Must not be called from the UI thread because the {@param currentUri} might refer to a not yet locally available file. - */ - @WorkerThread - public static File copyContentUriToTempFile(@NonNull Context context, @NonNull Uri currentUri, long accountId, Long localCardId) throws IOException, IllegalArgumentException { - final InputStream inputStream = context.getContentResolver().openInputStream(currentUri); - if (inputStream == null) { - throw new IOException("Could not open input stream for " + currentUri.getPath()); - } - final File cacheFile = getTempCacheFile(context, "attachments/account-" + accountId + "/card-" + (localCardId == null ? "pending-creation" : localCardId) + '/' + UriUtils.getDisplayNameForUri(currentUri, context)); - final FileOutputStream outputStream = new FileOutputStream(cacheFile); - byte[] buffer = new byte[4096]; - - int count; - while ((count = inputStream.read(buffer)) > 0) { - outputStream.write(buffer, 0, count); - } - DeckLog.verbose("----- wrote"); - return cacheFile; - } - - /** - * Creates a new {@link File} - */ - public static File getTempCacheFile(@NonNull Context context, String fileName) throws IOException { - File cacheFile = new File(context.getApplicationContext().getFilesDir().getAbsolutePath() + "/" + fileName); - - DeckLog.verbose("- Full path for new cache file: " + cacheFile.getAbsolutePath()); - - final File tempDir = cacheFile.getParentFile(); - if (tempDir == null) { - throw new FileNotFoundException("could not cacheFile.getParentFile()"); - } - if (!tempDir.exists()) { - DeckLog.verbose("-- The folder in which the new file should be created does not exist yet. Trying to create it..."); - if (tempDir.mkdirs()) { - DeckLog.verbose("--- Creation successful"); - } else { - throw new IOException("Directory for temporary file does not exist and could not be created."); - } - } - - DeckLog.verbose("- Try to create actual cache file"); - if (cacheFile.createNewFile()) { - DeckLog.verbose("-- Successfully created cache file"); - } else { - throw new IOException("Failed to create cacheFile"); - } - - return cacheFile; - } - @DrawableRes public static int getIconForMimeType(@NonNull String mimeType) { if (TextUtils.isEmpty(mimeType)) { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/FilesUtil.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/FilesUtil.java new file mode 100644 index 000000000..4847ac256 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/util/FilesUtil.java @@ -0,0 +1,78 @@ +package it.niedermann.nextcloud.deck.util; + +import android.content.Context; +import android.net.Uri; + +import androidx.annotation.NonNull; +import androidx.annotation.WorkerThread; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import it.niedermann.nextcloud.deck.DeckLog; + +/** + * Created by stefan on 07.03.20. + */ + +public class FilesUtil { + + private FilesUtil() { + } + + /** + * https://help.nextcloud.com/t/android-app-select-file-with-nextcloud-app-file-cant-be-read/103706 + * Must not be called from the UI thread because the {@param currentUri} might refer to a not yet locally available file. + */ + @WorkerThread + public static File copyContentUriToTempFile(@NonNull Context context, @NonNull Uri currentUri, long accountId, Long localCardId) throws IOException, IllegalArgumentException { + final InputStream inputStream = context.getContentResolver().openInputStream(currentUri); + if (inputStream == null) { + throw new IOException("Could not open input stream for " + currentUri.getPath()); + } + final File cacheFile = getTempCacheFile(context, "attachments/account-" + accountId + "/card-" + (localCardId == null ? "pending-creation" : localCardId) + '/' + UriUtils.getDisplayNameForUri(currentUri, context)); + final FileOutputStream outputStream = new FileOutputStream(cacheFile); + byte[] buffer = new byte[4096]; + + int count; + while ((count = inputStream.read(buffer)) > 0) { + outputStream.write(buffer, 0, count); + } + DeckLog.verbose("----- wrote"); + return cacheFile; + } + + /** + * Creates a new {@link File} + */ + public static File getTempCacheFile(@NonNull Context context, String fileName) throws IOException { + File cacheFile = new File(context.getApplicationContext().getFilesDir().getAbsolutePath() + "/" + fileName); + + DeckLog.verbose("- Full path for new cache file: " + cacheFile.getAbsolutePath()); + + final File tempDir = cacheFile.getParentFile(); + if (tempDir == null) { + throw new FileNotFoundException("could not cacheFile.getParentFile()"); + } + if (!tempDir.exists()) { + DeckLog.verbose("-- The folder in which the new file should be created does not exist yet. Trying to create it..."); + if (tempDir.mkdirs()) { + DeckLog.verbose("--- Creation successful"); + } else { + throw new IOException("Directory for temporary file does not exist and could not be created."); + } + } + + DeckLog.verbose("- Try to create actual cache file"); + if (cacheFile.createNewFile()) { + DeckLog.verbose("-- Successfully created cache file"); + } else { + throw new IOException("Failed to create cacheFile"); + } + + return cacheFile; + } +} |