DocumentManager.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "DocumentManager.h"
  21. #import "AppGroup.h"
  22. @implementation DocumentManager
  23. + (NSURL *)groupDocumentsDirectory
  24. {
  25. return [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[AppGroup groupId]];
  26. }
  27. + (NSURL *)applicationDocumentsDirectory
  28. {
  29. return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
  30. }
  31. + (NSURL *)cacheDirectory
  32. {
  33. return [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
  34. }
  35. + (NSURL *)documentsDirectory {
  36. return [self groupDocumentsDirectory];
  37. }
  38. + (NSURL *)databaseDirectory {
  39. return [self documentsDirectory];
  40. }
  41. + (unsigned long long)sizeOfObjectAtPath:(NSString*)path {
  42. /* Check if this is a file or directory */
  43. BOOL isDirectory = NO;
  44. if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory])
  45. return 0;
  46. if (!isDirectory) {
  47. /* file */
  48. NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
  49. return [fileDictionary fileSize];
  50. } else {
  51. /* directory */
  52. unsigned long long size = 0;
  53. NSDirectoryEnumerator *subPathsEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
  54. NSString *curSubpath;
  55. while (curSubpath = [subPathsEnumerator nextObject]) {
  56. NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:[path stringByAppendingPathComponent:curSubpath] error:nil];
  57. size += [fileDictionary fileSize];
  58. }
  59. return size;
  60. }
  61. }
  62. + (void)removeItemIfExists:(NSURL *)item {
  63. NSFileManager *fileManager = NSFileManager.defaultManager;
  64. if ([fileManager fileExistsAtPath:item.path]) {
  65. [fileManager removeItemAtURL:item error:nil];
  66. }
  67. }
  68. + (void)moveItemIfExists:(NSURL *)source destination:(NSURL *)destination {
  69. NSFileManager *fileManager = NSFileManager.defaultManager;
  70. if ([fileManager fileExistsAtPath:source.path]) {
  71. [fileManager moveItemAtURL:source toURL:destination error:nil];
  72. }
  73. }
  74. @end