CCUtility.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. //
  2. // CCUtility.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/02/16.
  6. // Copyright (c) 2016 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCUtility.h"
  24. #import "NCBridgeSwift.h"
  25. #import "NSNotificationCenter+MainThread.h"
  26. #import <OpenSSL/OpenSSL.h>
  27. #import <CoreLocation/CoreLocation.h>
  28. #include <sys/stat.h>
  29. #define INTRO_MessageType @"MessageType_"
  30. #define E2E_certificate @"EndToEndCertificate_"
  31. #define E2E_PrivateKey @"EndToEndPrivateKey_"
  32. #define E2E_Passphrase @"EndToEndPassphrase_"
  33. #define E2E_PublicKey @"EndToEndPublicKeyServer_"
  34. @implementation CCUtility
  35. #pragma --------------------------------------------------------------------------------------------
  36. #pragma mark ===== Various =====
  37. #pragma --------------------------------------------------------------------------------------------
  38. + (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
  39. {
  40. NSError *error = nil;
  41. BOOL success = [URL setResourceValue:[NSNumber numberWithBool: YES] forKey: NSURLIsExcludedFromBackupKey error: &error];
  42. if(success) {
  43. NSLog(@"Excluding %@ from backup", [URL lastPathComponent]);
  44. } else {
  45. NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
  46. }
  47. return success;
  48. }
  49. + (NSString *)dateDiff:(NSDate *) convertedDate
  50. {
  51. NSDate *todayDate = [NSDate date];
  52. double ti = [convertedDate timeIntervalSinceDate:todayDate];
  53. ti = ti * -1;
  54. if (ti < 60) {
  55. return NSLocalizedString(@"_less_a_minute_", nil);
  56. } else if (ti < 3600) {
  57. int diff = round(ti / 60);
  58. if (diff == 1) {
  59. return NSLocalizedString(@"_a_minute_ago_", nil);
  60. } else {
  61. return [NSString stringWithFormat:NSLocalizedString(@"_minutes_ago_", nil), diff];
  62. }
  63. } else if (ti < 86400) {
  64. int diff = round(ti / 60 / 60);
  65. if (diff == 1) {
  66. return NSLocalizedString(@"_an_hour_ago_", nil);
  67. } else {
  68. return[NSString stringWithFormat:NSLocalizedString(@"_hours_ago_", nil), diff];
  69. }
  70. } else if (ti < 86400 * 30) {
  71. int diff = round(ti / 60 / 60 / 24);
  72. if (diff == 1) {
  73. return NSLocalizedString(@"_a_day_ago_", nil);
  74. } else {
  75. return[NSString stringWithFormat:NSLocalizedString(@"_days_ago_", nil), diff];
  76. }
  77. } else {
  78. // Older than one month
  79. NSDateFormatter *df = [[NSDateFormatter alloc] init];
  80. [df setFormatterBehavior:NSDateFormatterBehavior10_4];
  81. [df setDateStyle:NSDateFormatterMediumStyle];
  82. return [df stringFromDate:convertedDate];
  83. }
  84. }
  85. + (NSString *)transformedSize:(int64_t)value
  86. {
  87. NSString *string = [NSByteCountFormatter stringFromByteCount:value countStyle:NSByteCountFormatterCountStyleBinary];
  88. return string;
  89. }
  90. // Remove do not forbidden characters for Nextcloud Server
  91. + (NSString *)removeForbiddenCharactersServer:(NSString *)fileName
  92. {
  93. NSArray *arrayForbiddenCharacters = [NSArray arrayWithObjects:@"/", nil];
  94. for (NSString *currentCharacter in arrayForbiddenCharacters) {
  95. fileName = [fileName stringByReplacingOccurrencesOfString:currentCharacter withString:@""];
  96. }
  97. return fileName;
  98. }
  99. + (NSString*)stringAppendServerUrl:(NSString *)serverUrl addFileName:(NSString *)addFileName
  100. {
  101. NSString *result;
  102. if (serverUrl == nil || addFileName == nil) return nil;
  103. if ([addFileName isEqualToString:@""]) return serverUrl;
  104. if ([serverUrl isEqualToString:@"/"]) result = [serverUrl stringByAppendingString:addFileName];
  105. else result = [NSString stringWithFormat:@"%@/%@", serverUrl, addFileName];
  106. return result;
  107. }
  108. + (NSString *)createFileNameDate:(NSString *)fileName extension:(NSString *)extension
  109. {
  110. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  111. [formatter setDateFormat:@"yy-MM-dd HH-mm-ss"];
  112. NSString *fileNameDate = [formatter stringFromDate:[NSDate date]];
  113. NSString *returnFileName;
  114. if ([fileName isEqualToString:@""] && ![extension isEqualToString:@""]) {
  115. returnFileName = [NSString stringWithFormat:@"%@.%@", fileNameDate, extension];
  116. }
  117. if (![fileName isEqualToString:@""] && [extension isEqualToString:@""]) {
  118. returnFileName = [NSString stringWithFormat:@"%@ %@", fileName, fileNameDate];
  119. }
  120. if ([fileName isEqualToString:@""] && [extension isEqualToString:@""]) {
  121. returnFileName = fileNameDate;
  122. }
  123. if (![fileName isEqualToString:@""] && ![extension isEqualToString:@""]) {
  124. returnFileName = [NSString stringWithFormat:@"%@ %@.%@", fileName, fileNameDate, extension];
  125. }
  126. return returnFileName;
  127. }
  128. + (NSString *)createFileName:(NSString *)fileName fileDate:(NSDate *)fileDate fileType:(PHAssetMediaType)fileType keyFileName:(NSString *)keyFileName keyFileNameType:(NSString *)keyFileNameType keyFileNameOriginal:(NSString *)keyFileNameOriginal forcedNewFileName:(BOOL)forcedNewFileName
  129. {
  130. BOOL addFileNameType = NO;
  131. // Original FileName ?
  132. if ([[[NCKeychain alloc] init] getOriginalFileNameWithKey:keyFileNameOriginal] && !forcedNewFileName) {
  133. return fileName;
  134. }
  135. NSString *numberFileName;
  136. if ([fileName length] > 8) numberFileName = [fileName substringWithRange:NSMakeRange(04, 04)];
  137. else numberFileName = [NCKeychain alloc].incrementalNumber;
  138. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  139. [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  140. [formatter setDateFormat:@"yy-MM-dd HH-mm-ss"];
  141. NSString *fileNameDate = [formatter stringFromDate:fileDate];
  142. NSString *fileNameType = @"";
  143. if (fileType == PHAssetMediaTypeImage)
  144. fileNameType = NSLocalizedString(@"_photo_", nil);
  145. if (fileType == PHAssetMediaTypeVideo)
  146. fileNameType = NSLocalizedString(@"_video_", nil);
  147. if (fileType == PHAssetMediaTypeAudio)
  148. fileNameType = NSLocalizedString(@"_audio_", nil);
  149. if (fileType == PHAssetMediaTypeUnknown)
  150. fileNameType = NSLocalizedString(@"_unknown_", nil);
  151. // Use File Name Type
  152. if (keyFileNameType)
  153. addFileNameType = [[[NCKeychain alloc] init] getFileNameTypeWithKey:keyFileNameType];
  154. NSString *fileNameExt = [[fileName pathExtension] lowercaseString];
  155. if (keyFileName) {
  156. fileName = [[[NCKeychain alloc] init] getFileNameMaskWithKey:keyFileName];
  157. if ([fileName length] > 0) {
  158. [formatter setDateFormat:@"dd"];
  159. NSString *dayNumber = [formatter stringFromDate:fileDate];
  160. [formatter setDateFormat:@"MMM"];
  161. NSString *month = [formatter stringFromDate:fileDate];
  162. [formatter setDateFormat:@"MM"];
  163. NSString *monthNumber = [formatter stringFromDate:fileDate];
  164. [formatter setDateFormat:@"yyyy"];
  165. NSString *year = [formatter stringFromDate:fileDate];
  166. [formatter setDateFormat:@"yy"];
  167. NSString *yearNumber = [formatter stringFromDate:fileDate];
  168. [formatter setDateFormat:@"HH"];
  169. NSString *hour24 = [formatter stringFromDate:fileDate];
  170. [formatter setDateFormat:@"hh"];
  171. NSString *hour12 = [formatter stringFromDate:fileDate];
  172. [formatter setDateFormat:@"mm"];
  173. NSString *minute = [formatter stringFromDate:fileDate];
  174. [formatter setDateFormat:@"ss"];
  175. NSString *second = [formatter stringFromDate:fileDate];
  176. [formatter setDateFormat:@"a"];
  177. NSString *ampm = [formatter stringFromDate:fileDate];
  178. // Replace string with date
  179. fileName = [fileName stringByReplacingOccurrencesOfString:@"DD" withString:dayNumber];
  180. fileName = [fileName stringByReplacingOccurrencesOfString:@"MMM" withString:month];
  181. fileName = [fileName stringByReplacingOccurrencesOfString:@"MM" withString:monthNumber];
  182. fileName = [fileName stringByReplacingOccurrencesOfString:@"YYYY" withString:year];
  183. fileName = [fileName stringByReplacingOccurrencesOfString:@"YY" withString:yearNumber];
  184. fileName = [fileName stringByReplacingOccurrencesOfString:@"HH" withString:hour24];
  185. fileName = [fileName stringByReplacingOccurrencesOfString:@"hh" withString:hour12];
  186. fileName = [fileName stringByReplacingOccurrencesOfString:@"mm" withString:minute];
  187. fileName = [fileName stringByReplacingOccurrencesOfString:@"ss" withString:second];
  188. fileName = [fileName stringByReplacingOccurrencesOfString:@"ampm" withString:ampm];
  189. if (addFileNameType)
  190. fileName = [NSString stringWithFormat:@"%@%@%@.%@", fileNameType, fileName, numberFileName, fileNameExt];
  191. else
  192. fileName = [NSString stringWithFormat:@"%@%@.%@", fileName, numberFileName, fileNameExt];
  193. fileName = [fileName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  194. } else {
  195. if (addFileNameType)
  196. fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
  197. else
  198. fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
  199. }
  200. } else {
  201. if (addFileNameType)
  202. fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
  203. else
  204. fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
  205. }
  206. return fileName;
  207. }
  208. + (void)createDirectoryStandard
  209. {
  210. NSString *path;
  211. NSURL *dirGroup = [CCUtility getDirectoryGroup];
  212. NSLog(@"[LOG] Dir Group");
  213. NSLog(@"%@", [dirGroup path]);
  214. NSLog(@"[LOG] Program application ");
  215. NSLog(@"%@", [[CCUtility getDirectoryDocuments] stringByDeletingLastPathComponent]);
  216. // create Directory Documents
  217. path = [CCUtility getDirectoryDocuments];
  218. if (![[NSFileManager defaultManager] fileExistsAtPath: path])
  219. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  220. // create Directory audio => Library, Application Support, audio
  221. path = [CCUtility getDirectoryAudio];
  222. if (![[NSFileManager defaultManager] fileExistsAtPath: path])
  223. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  224. // create Directory database Nextcloud
  225. path = [[dirGroup URLByAppendingPathComponent:[[NCGlobal shared] appDatabaseNextcloud]] path];
  226. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  227. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  228. // create Directory User Data
  229. path = [[dirGroup URLByAppendingPathComponent:NCGlobal.shared.appUserData] path];
  230. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  231. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  232. // create Directory Provider Storage
  233. path = [CCUtility getDirectoryProviderStorage];
  234. if (![[NSFileManager defaultManager] fileExistsAtPath: path])
  235. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  236. // create Directory Scan
  237. path = [[dirGroup URLByAppendingPathComponent:NCGlobal.shared.appScan] path];
  238. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  239. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  240. // create Directory Temp
  241. path = NSTemporaryDirectory();
  242. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  243. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  244. // Directory Excluded From Backup
  245. [CCUtility addSkipBackupAttributeToItemAtURL:[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]];
  246. [CCUtility addSkipBackupAttributeToItemAtURL:[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.directoryProviderStorage]];
  247. [CCUtility addSkipBackupAttributeToItemAtURL:[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appApplicationSupport]];
  248. [CCUtility addSkipBackupAttributeToItemAtURL:[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appCertificates]];
  249. [CCUtility addSkipBackupAttributeToItemAtURL:[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appDatabaseNextcloud]];
  250. [CCUtility addSkipBackupAttributeToItemAtURL:[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appScan]];
  251. [CCUtility addSkipBackupAttributeToItemAtURL:[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appUserData]];
  252. }
  253. + (NSURL *)getDirectoryGroup
  254. {
  255. NSURL *path = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[NCBrandOptions shared].capabilitiesGroups];
  256. return path;
  257. }
  258. // Return the path of directory Documents -> NSDocumentDirectory
  259. + (NSString *)getDirectoryDocuments
  260. {
  261. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  262. return [paths objectAtIndex:0];
  263. }
  264. + (NSString *)getDirectoryReaderMetadata
  265. {
  266. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  267. return [NSString stringWithFormat:@"%@/Reader Metadata", [paths objectAtIndex:0]];
  268. }
  269. // Return the path of directory Audio
  270. + (NSString *)getDirectoryAudio
  271. {
  272. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  273. return [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], @"audio"];
  274. }
  275. // Return the path of directory Cetificates
  276. + (NSString *)getDirectoryCerificates
  277. {
  278. NSString *path = [[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appCertificates] path];
  279. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  280. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  281. return path;
  282. }
  283. + (NSString *)getDirectoryUserData
  284. {
  285. NSString *path = [[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appUserData] path];
  286. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  287. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  288. return path;
  289. }
  290. + (NSString *)getDirectoryProviderStorage
  291. {
  292. NSString *path = [[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.directoryProviderStorage] path];
  293. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  294. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  295. return path;
  296. }
  297. + (NSString *)getDirectoryProviderStorageOcId:(NSString *)ocId
  298. {
  299. NSString *path = [NSString stringWithFormat:@"%@/%@", [self getDirectoryProviderStorage], ocId];
  300. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  301. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  302. return path;
  303. }
  304. + (NSString *)getDirectoryProviderStorageOcId:(NSString *)ocId fileNameView:(NSString *)fileNameView
  305. {
  306. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@", [self getDirectoryProviderStorageOcId:ocId], fileNameView];
  307. // if do not exists create file 0 length
  308. // causes files with lenth 0 to never be downloaded, because already exist
  309. // also makes it impossible to delete any file with length 0 (from cache)
  310. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath] == NO) {
  311. [[NSFileManager defaultManager] createFileAtPath:fileNamePath contents:nil attributes:nil];
  312. }
  313. return fileNamePath;
  314. }
  315. + (NSString *)getDirectoryProviderStorageIconOcId:(NSString *)ocId etag:(NSString *)etag
  316. {
  317. return [NSString stringWithFormat:@"%@/%@.small.%@", [self getDirectoryProviderStorageOcId:ocId], etag, [NCGlobal shared].extensionPreview];
  318. }
  319. + (NSString *)getDirectoryProviderStoragePreviewOcId:(NSString *)ocId etag:(NSString *)etag
  320. {
  321. return [NSString stringWithFormat:@"%@/%@.preview.%@", [self getDirectoryProviderStorageOcId:ocId], etag, [NCGlobal shared].extensionPreview];
  322. }
  323. + (BOOL)fileProviderStorageExists:(tableMetadata *)metadata
  324. {
  325. NSString *fileNameViewPath = [self getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileNameView];
  326. NSString *fileNamePath = [self getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileName];
  327. unsigned long long fileNameViewSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:fileNameViewPath error:nil] fileSize];
  328. unsigned long long fileNameSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:fileNamePath error:nil] fileSize];
  329. if (metadata.isDirectoryE2EE == true) {
  330. if ((fileNameSize == metadata.size || fileNameViewSize == metadata.size) && fileNameViewSize > 0) {
  331. return true;
  332. } else {
  333. return false;
  334. }
  335. } else {
  336. return fileNameViewSize == metadata.size;
  337. }
  338. }
  339. + (int64_t)fileProviderStorageSize:(NSString *)ocId fileNameView:(NSString *)fileNameView
  340. {
  341. NSString *fileNamePath = [self getDirectoryProviderStorageOcId:ocId fileNameView:fileNameView];
  342. int64_t fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:fileNamePath error:nil] fileSize];
  343. return fileSize;
  344. }
  345. + (BOOL)fileProviderStoragePreviewIconExists:(NSString *)ocId etag:(NSString *)etag
  346. {
  347. NSString *fileNamePathPreview = [self getDirectoryProviderStoragePreviewOcId:ocId etag:etag];
  348. NSString *fileNamePathIcon = [self getDirectoryProviderStorageIconOcId:ocId etag:etag];
  349. unsigned long long fileSizePreview = [[[NSFileManager defaultManager] attributesOfItemAtPath:fileNamePathPreview error:nil] fileSize];
  350. unsigned long long fileSizeIcon = [[[NSFileManager defaultManager] attributesOfItemAtPath:fileNamePathIcon error:nil] fileSize];
  351. if (fileSizePreview > 0 && fileSizeIcon > 0) return true;
  352. else return false;
  353. }
  354. + (void)removeGroupApplicationSupport
  355. {
  356. NSURL *dirGroup = [CCUtility getDirectoryGroup];
  357. NSString *path = [[dirGroup URLByAppendingPathComponent:NCGlobal.shared.appApplicationSupport] path];
  358. [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
  359. }
  360. + (void)removeGroupLibraryDirectory
  361. {
  362. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryScan] error:nil];
  363. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryUserData] error:nil];
  364. }
  365. + (void)removeGroupDirectoryProviderStorage
  366. {
  367. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorage] error:nil];
  368. }
  369. + (void)removeDocumentsDirectory
  370. {
  371. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryDocuments] error:nil];
  372. }
  373. + (void)removeTemporaryDirectory
  374. {
  375. [[NSFileManager defaultManager] removeItemAtPath:NSTemporaryDirectory() error:nil];
  376. }
  377. + (void)emptyTemporaryDirectory
  378. {
  379. NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
  380. for (NSString *file in tmpDirectory) {
  381. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
  382. }
  383. }
  384. + (NSString *)getTitleSectionDate:(NSDate *)date
  385. {
  386. NSString *title;
  387. NSDate *today = [NSDate date];
  388. NSDate *yesterday = [today dateByAddingTimeInterval: -86400.0];
  389. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:[NSDate distantPast]]]) {
  390. title = NSLocalizedString(@"_no_date_", nil);
  391. } else {
  392. title = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterLongStyle timeStyle:0];
  393. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:today]])
  394. title = [NSString stringWithFormat:NSLocalizedString(@"_today_", nil)];
  395. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:yesterday]])
  396. title = [NSString stringWithFormat:NSLocalizedString(@"_yesterday_", nil)];
  397. }
  398. return title;
  399. }
  400. + (void)moveFileAtPath:(NSString *)atPath toPath:(NSString *)toPath
  401. {
  402. [[NSFileManager defaultManager] removeItemAtPath:toPath error:nil];
  403. [[NSFileManager defaultManager] copyItemAtPath:atPath toPath:toPath error:nil];
  404. [[NSFileManager defaultManager] removeItemAtPath:atPath error:nil];
  405. }
  406. + (void)copyFileAtPath:(NSString *)atPath toPath:(NSString *)toPath
  407. {
  408. [[NSFileManager defaultManager] removeItemAtPath:toPath error:nil];
  409. [[NSFileManager defaultManager] copyItemAtPath:atPath toPath:toPath error:nil];
  410. }
  411. + (void)removeFileAtPath:(NSString *)atPath
  412. {
  413. [[NSFileManager defaultManager] removeItemAtPath:atPath error:nil];
  414. }
  415. + (void)createDirectoryAtPath:(NSString *)atPath
  416. {
  417. [[NSFileManager defaultManager] createDirectoryAtPath:atPath withIntermediateDirectories:true attributes:nil error:nil];
  418. }
  419. + (NSString *)returnPathfromServerUrl:(NSString *)serverUrl urlBase:(NSString *)urlBase userId:(NSString *)userId account:(NSString *)account
  420. {
  421. NSString *homeServer = [[NCUtilityFileSystem shared] getHomeServerWithUrlBase:urlBase userId:userId];
  422. NSString *path = [serverUrl stringByReplacingOccurrencesOfString:homeServer withString:@""];
  423. return path;
  424. }
  425. + (NSString *)returnFileNamePathFromFileName:(NSString *)metadataFileName serverUrl:(NSString *)serverUrl urlBase:(NSString *)urlBase userId:(NSString *)userId account:(NSString *)account
  426. {
  427. if (metadataFileName == nil || serverUrl == nil || urlBase == nil) {
  428. return @"";
  429. }
  430. NSString *homeServer = [[NCUtilityFileSystem shared] getHomeServerWithUrlBase:urlBase userId:userId];
  431. NSString *fileName = [NSString stringWithFormat:@"%@/%@", [serverUrl stringByReplacingOccurrencesOfString:homeServer withString:@""], metadataFileName];
  432. if ([fileName hasPrefix:@"/"]) fileName = [fileName substringFromIndex:1];
  433. return fileName;
  434. }
  435. + (NSString *)getMimeType:(NSString *)fileNameView
  436. {
  437. CFStringRef fileUTI = nil;
  438. NSString *returnFileUTI = nil;
  439. if ([fileNameView isEqualToString:@"."]) {
  440. return returnFileUTI;
  441. } else {
  442. CFStringRef fileExtension = (__bridge CFStringRef)[fileNameView pathExtension];
  443. NSString *ext = (__bridge NSString *)fileExtension;
  444. ext = ext.uppercaseString;
  445. fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  446. if (fileUTI != nil) {
  447. returnFileUTI = (__bridge NSString *)fileUTI;
  448. CFRelease(fileUTI);
  449. }
  450. }
  451. return returnFileUTI;
  452. }
  453. + (NSString *)getDirectoryScan
  454. {
  455. NSString *path = [[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appScan] path];
  456. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  457. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  458. return path;
  459. }
  460. #pragma --------------------------------------------------------------------------------------------
  461. #pragma mark ===== Share Permissions =====
  462. #pragma --------------------------------------------------------------------------------------------
  463. + (NSInteger) getPermissionsValueByCanEdit:(BOOL)canEdit andCanCreate:(BOOL)canCreate andCanChange:(BOOL)canChange andCanDelete:(BOOL)canDelete andCanShare:(BOOL)canShare andIsFolder:(BOOL) isFolder
  464. {
  465. NSInteger permissionsValue = NCGlobal.shared.permissionReadShare;
  466. if (canEdit && !isFolder) {
  467. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  468. }
  469. if (canCreate & isFolder) {
  470. permissionsValue = permissionsValue + NCGlobal.shared.permissionCreateShare;
  471. }
  472. if (canChange && isFolder) {
  473. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  474. }
  475. if (canDelete & isFolder) {
  476. permissionsValue = permissionsValue + NCGlobal.shared.permissionDeleteShare;
  477. }
  478. if (canShare) {
  479. permissionsValue = permissionsValue + NCGlobal.shared.permissionShareShare;
  480. }
  481. return permissionsValue;
  482. }
  483. + (BOOL) isPermissionToCanCreate:(NSInteger) permissionValue {
  484. BOOL canCreate = ((permissionValue & NCGlobal.shared.permissionCreateShare) > 0);
  485. return canCreate;
  486. }
  487. + (BOOL) isPermissionToCanChange:(NSInteger) permissionValue {
  488. BOOL canChange = ((permissionValue & NCGlobal.shared.permissionUpdateShare) > 0);
  489. return canChange;
  490. }
  491. + (BOOL) isPermissionToCanDelete:(NSInteger) permissionValue {
  492. BOOL canDelete = ((permissionValue & NCGlobal.shared.permissionDeleteShare) > 0);
  493. return canDelete;
  494. }
  495. + (BOOL) isPermissionToCanShare:(NSInteger) permissionValue {
  496. BOOL canShare = ((permissionValue & NCGlobal.shared.permissionShareShare) > 0);
  497. return canShare;
  498. }
  499. + (BOOL) isAnyPermissionToEdit:(NSInteger) permissionValue {
  500. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  501. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  502. BOOL canDelete = [self isPermissionToCanDelete:permissionValue];
  503. BOOL canEdit = (canCreate || canChange || canDelete);
  504. return canEdit;
  505. }
  506. + (BOOL) isPermissionToRead:(NSInteger) permissionValue {
  507. BOOL canRead = ((permissionValue & NCGlobal.shared.permissionReadShare) > 0);
  508. return canRead;
  509. }
  510. + (BOOL) isPermissionToReadCreateUpdate:(NSInteger) permissionValue {
  511. BOOL canRead = [self isPermissionToRead:permissionValue];
  512. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  513. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  514. BOOL canEdit = (canCreate && canChange && canRead);
  515. return canEdit;
  516. }
  517. #pragma --------------------------------------------------------------------------------------------
  518. #pragma mark ===== Third parts =====
  519. #pragma --------------------------------------------------------------------------------------------
  520. + (NSString *)getExtension:(NSString*)fileName
  521. {
  522. NSMutableArray *fileNameArray =[[NSMutableArray alloc] initWithArray: [fileName componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]]];
  523. NSString *extension = [NSString stringWithFormat:@"%@",[fileNameArray lastObject]];
  524. extension = [extension uppercaseString];
  525. //If the file has a ZIP extension obtain the previous one for check if it is a .pages.zip / .numbers.zip / .key.zip extension
  526. if ([extension isEqualToString:@"ZIP"]) {
  527. [fileNameArray removeLastObject];
  528. NSString *secondExtension = [NSString stringWithFormat:@"%@",[fileNameArray lastObject]];
  529. secondExtension = [secondExtension uppercaseString];
  530. if ([secondExtension isEqualToString:@"PAGES"] || [secondExtension isEqualToString:@"NUMBERS"] || [secondExtension isEqualToString:@"KEY"]) {
  531. extension = [NSString stringWithFormat:@"%@.%@",secondExtension,extension];
  532. return extension;
  533. }
  534. }
  535. return extension;
  536. }
  537. + (NSDate *)datetimeWithOutTime:(NSDate *)datDate
  538. {
  539. if (datDate == nil) return nil;
  540. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:datDate];
  541. datDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
  542. return datDate;
  543. }
  544. + (NSString *)valueForKey:(NSString *)key fromQueryItems:(NSArray *)queryItems
  545. {
  546. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", key];
  547. NSURLQueryItem *queryItem = [[queryItems filteredArrayUsingPredicate:predicate] firstObject];
  548. return queryItem.value;
  549. }
  550. + (NSDate *)getATime:(const char *)path
  551. {
  552. struct stat st;
  553. stat(path, &st);
  554. time_t accessed = st.st_atime;
  555. NSDate *date = [NSDate dateWithTimeIntervalSince1970:accessed];
  556. return date;
  557. }
  558. @end