Fix downloading/exporting when overwriting file would not truncate

This commit is contained in:
Stypox 2024-11-24 18:34:22 +01:00
parent 57e66b17c6
commit df941670a8
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
3 changed files with 15 additions and 3 deletions

View File

@ -76,7 +76,7 @@ public class SubscriptionsExportService extends BaseImportExportService {
try {
outFile = new StoredFileHelper(this, path, "application/json");
outputStream = new SharpOutputStream(outFile.getStream());
outputStream = new SharpOutputStream(outFile.openAndTruncateStream());
} catch (final IOException e) {
handleError(e);
return START_NOT_STICKY;

View File

@ -24,8 +24,7 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
*/
@Throws(Exception::class)
fun exportDatabase(preferences: SharedPreferences, file: StoredFileHelper) {
file.create()
ZipOutputStream(SharpOutputStream(file.stream).buffered()).use { outZip ->
ZipOutputStream(SharpOutputStream(file.openAndTruncateStream()).buffered()).use { outZip ->
// add the database
ZipHelper.addFileToZip(
outZip,

View File

@ -189,6 +189,19 @@ public class StoredFileHelper implements Serializable {
}
}
public SharpStream openAndTruncateStream() throws IOException {
final SharpStream sharpStream = getStream();
try {
sharpStream.setLength(0);
} catch (final Throwable e) {
// we can't use try-with-resources here, since we only want to close the stream if an
// exception occurs, but leave it open if everything goes well
sharpStream.close();
throw e;
}
return sharpStream;
}
/**
* Indicates whether it's using the {@code java.io} API.
*