tdf#133278: Don't overwrite the copy of the document that we are editing

When exporting a copy, let core write the copy to a temp subdirectory
before invoking UIDocumentPickerViewController to select where to
store it permanently.

Change-Id: I3d2292414a3c824515ba6d98ad09b296e543cea9
Reviewed-on: https://gerrit.libreoffice.org/c/online/+/95295
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Tor Lillqvist <tml@collabora.com>
diff --git a/ios/Mobile/DocumentViewController.mm b/ios/Mobile/DocumentViewController.mm
index 54932be..c94161c 100644
--- a/ios/Mobile/DocumentViewController.mm
+++ b/ios/Mobile/DocumentViewController.mm
@@ -16,6 +16,7 @@
#import <objc/runtime.h>

#import <poll.h>
#import <sys/stat.h>

#import "ios.h"
#import "FakeSocket.hpp"
@@ -465,8 +466,13 @@
                // First save it in the requested format to a temporary location. First remove any
                // leftover identically named temporary file.

                NSURL *tmpFileDirectory = [[NSFileManager.defaultManager temporaryDirectory] URLByAppendingPathComponent:@"export"];
                if (![NSFileManager.defaultManager createDirectoryAtURL:tmpFileDirectory withIntermediateDirectories:YES attributes:nil error:nil]) {
                    LOG_ERR("Could not create directory " << [[tmpFileDirectory path] UTF8String]);
                    return;
                }
                NSString *tmpFileName = [[[self.document->copyFileURL lastPathComponent] stringByDeletingPathExtension] stringByAppendingString:[@"." stringByAppendingString:format]];
                downloadAsTmpURL = [[NSFileManager.defaultManager temporaryDirectory] URLByAppendingPathComponent:tmpFileName];
                downloadAsTmpURL = [tmpFileDirectory URLByAppendingPathComponent:tmpFileName];

                std::remove([[downloadAsTmpURL path] UTF8String]);

@@ -479,15 +485,15 @@
                struct stat statBuf;
                if (stat([[downloadAsTmpURL path] UTF8String], &statBuf) == -1) {
                    LOG_ERR("Could apparently not save to '" <<  [[downloadAsTmpURL path] UTF8String] << "'");
                } else {
                    UIDocumentPickerViewController *picker =
                        [[UIDocumentPickerViewController alloc] initWithURL:downloadAsTmpURL
                                                                     inMode:UIDocumentPickerModeExportToService];
                    picker.delegate = self;
                    [self presentViewController:picker
                                       animated:YES
                                     completion:nil];
                    return;
                }
                UIDocumentPickerViewController *picker =
                    [[UIDocumentPickerViewController alloc] initWithURL:downloadAsTmpURL
                                                                 inMode:UIDocumentPickerModeExportToService];
                picker.delegate = self;
                [self presentViewController:picker
                                   animated:YES
                                 completion:nil];
                return;
            }
        }
@@ -504,10 +510,12 @@

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
    std::remove([[downloadAsTmpURL path] UTF8String]);
    std::remove([[[downloadAsTmpURL URLByDeletingLastPathComponent] path] UTF8String]);
}

- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
    std::remove([[downloadAsTmpURL path] UTF8String]);
    std::remove([[[downloadAsTmpURL URLByDeletingLastPathComponent] path] UTF8String]);
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {