Skip to content

Commit

Permalink
Add setting to not link filetransfers into documents directory
Browse files Browse the repository at this point in the history
  • Loading branch information
tmolitor-stud-tu committed Jul 31, 2024
1 parent 9717831 commit f194b05
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
8 changes: 7 additions & 1 deletion Monal/Classes/GeneralSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class GeneralSettingsDefaultsDB: ObservableObject {

@defaultsDB("uploadImagesOriginal")
var uploadImagesOriginal: Bool

@defaultsDB("hardlinkFiletransfersIntoDocuments")
var hardlinkFiletransfersIntoDocuments: Bool
}


Expand Down Expand Up @@ -457,7 +460,10 @@ struct AttachmentSettings: View {
Form {
Section(header: Text("General File Transfer Settings")) {
SettingsToggle(isOn: $generalSettingsDefaultsDB.autodownloadFiletransfers) {
Text("Auto-Download Media")
Text("Auto-Download Media and Files")
}
SettingsToggle(isOn: $generalSettingsDefaultsDB.hardlinkFiletransfersIntoDocuments) {
Text("Make transfered Media and Files accessible in Files App")
}
}

Expand Down
61 changes: 32 additions & 29 deletions Monal/Classes/MLFiletransfer.m
Original file line number Diff line number Diff line change
Expand Up @@ -358,67 +358,70 @@ -(void) URLSession:(nonnull NSURLSession*) session downloadTask:(nonnull NSURLSe
if(error)
{
DDLogError(@"Could not copy cache file to tmp file: %@", error);
#ifdef DEBUG
#ifdef DEBUG
@throw [NSException exceptionWithName:@"ERROR_WHILE_COPYING_CACHEFILE" reason:@"Could not copy cacheFile!" userInfo:@{
@"cacheFile": cacheFile,
@"cacheFileTMP": cacheFileTMP
}];
#endif
#endif
return;
}

[_fileManager removeItemAtPath:cacheFile error:&error];
if(error)
{
DDLogError(@"Could not delete original cache file: %@", error);
#ifdef DEBUG
#ifdef DEBUG
@throw [NSException exceptionWithName:@"ERROR_WHILE_DELETING_CACHEFILE" reason:@"Could not delete cacheFile!" userInfo:@{
@"cacheFile": cacheFile
}];
#endif
#endif
return;
}

[_fileManager moveItemAtPath:cacheFileTMP toPath:cacheFile error:&error];
if(error)
{
DDLogError(@"Could not rename tmp file to cache file: %@", error);
#ifdef DEBUG
#ifdef DEBUG
@throw [NSException exceptionWithName:@"ERROR_WHILE_RENAMING_CACHEFILE" reason:@"Could not rename cacheFileTMP to cacheFile!" userInfo:@{
@"cacheFile": cacheFile,
@"cacheFileTMP": cacheFileTMP
}];
#endif
#endif
return;
}
}

NSURL* hardLink = [[_fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
for(NSString* pathComponent in hardlinkPathComponents)
hardLink = [hardLink URLByAppendingPathComponent:pathComponent];

DDLogInfo(@"Hardlinking cache file at '%@' into documents directory at '%@'...", cacheFile, hardLink);
if(![_fileManager fileExistsAtPath:[hardLink.URLByDeletingLastPathComponent path]])
if([[HelperTools defaultsDB] boolForKey:@"hardlinkFiletransfersIntoDocuments"])
{
DDLogVerbose(@"Creating hardlinking dir struct at '%@'...", hardLink.URLByDeletingLastPathComponent);
[_fileManager createDirectoryAtURL:hardLink.URLByDeletingLastPathComponent withIntermediateDirectories:YES attributes:@{NSFileProtectionKey: NSFileProtectionCompleteUntilFirstUserAuthentication} error:&error];
if(error)
DDLogWarn(@"Ignoring error creating hardlinking dir struct at '%@': %@", hardLink, error);
NSURL* hardLink = [[_fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
for(NSString* pathComponent in hardlinkPathComponents)
hardLink = [hardLink URLByAppendingPathComponent:pathComponent];

DDLogInfo(@"Hardlinking cache file at '%@' into documents directory at '%@'...", cacheFile, hardLink);
if(![_fileManager fileExistsAtPath:[hardLink.URLByDeletingLastPathComponent path]])
{
DDLogVerbose(@"Creating hardlinking dir struct at '%@'...", hardLink.URLByDeletingLastPathComponent);
[_fileManager createDirectoryAtURL:hardLink.URLByDeletingLastPathComponent withIntermediateDirectories:YES attributes:@{NSFileProtectionKey: NSFileProtectionCompleteUntilFirstUserAuthentication} error:&error];
if(error)
DDLogWarn(@"Ignoring error creating hardlinking dir struct at '%@': %@", hardLink, error);
else
[HelperTools configureFileProtection:NSFileProtectionCompleteUntilFirstUserAuthentication forFile:[hardLink path]];
}

//don't throw any error if the file aready exists, because it could be a rare collision (we only use 16 bit random numbers to keep the file prefix short)
if([_fileManager fileExistsAtPath:[hardLink path]])
DDLogWarn(@"Not hardlinking file '%@' to '%@': file already exists (maybe a rare collision?)...", cacheFile, hardLink);
else
[HelperTools configureFileProtection:NSFileProtectionCompleteUntilFirstUserAuthentication forFile:[hardLink path]];
}

//don't throw any error if the file aready exists, because it could be a rare collision (we only use 16 bit random numbers to keep the file prefix short)
if([_fileManager fileExistsAtPath:[hardLink path]])
DDLogWarn(@"Not hardlinking file '%@' to '%@': file already exists (maybe a rare collision?)...", cacheFile, hardLink);
else
{
DDLogVerbose(@"Hardlinking cache file '%@' to '%@'...", cacheFile, hardLink);
error = [HelperTools hardLinkOrCopyFile:cacheFile to:[hardLink path]];
if(error)
{
DDLogError(@"Error creating hardlink: %@", error);
@throw [NSException exceptionWithName:@"ERROR_WHILE_HARDLINKING_FILE" reason:[NSString stringWithFormat:@"%@", error] userInfo:@{@"error": error}];
DDLogVerbose(@"Hardlinking cache file '%@' to '%@'...", cacheFile, hardLink);
error = [HelperTools hardLinkOrCopyFile:cacheFile to:[hardLink path]];
if(error)
{
DDLogError(@"Error creating hardlink: %@", error);
@throw [NSException exceptionWithName:@"ERROR_WHILE_HARDLINKING_FILE" reason:[NSString stringWithFormat:@"%@", error] userInfo:@{@"error": error}];
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Monal/Classes/MLXMPPManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ -(void) defaultSettings

[self upgradeBoolUserSettingsIfUnset:@"uploadImagesOriginal" toDefault:NO];

[self upgradeBoolUserSettingsIfUnset:@"hardlinkFiletransfersIntoDocuments" toDefault:YES];

// //always show onboarding on simulator for now
// #if TARGET_OS_SIMULATOR
// [[HelperTools defaultsDB] setBool:NO forKey:@"hasCompletedOnboarding"];
Expand Down

0 comments on commit f194b05

Please sign in to comment.