CCUtility.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. // Remove do not forbidden characters for File System Server
  100. + (NSString *)removeForbiddenCharactersFileSystem:(NSString *)fileName
  101. {
  102. NSArray *arrayForbiddenCharacters = [NSArray arrayWithObjects:@"\\",@"<",@">",@":",@"\"",@"|",@"?",@"*",@"/", nil];
  103. for (NSString *currentCharacter in arrayForbiddenCharacters) {
  104. fileName = [fileName stringByReplacingOccurrencesOfString:currentCharacter withString:@""];
  105. }
  106. return fileName;
  107. }
  108. + (NSString*)stringAppendServerUrl:(NSString *)serverUrl addFileName:(NSString *)addFileName
  109. {
  110. NSString *result;
  111. if (serverUrl == nil || addFileName == nil) return nil;
  112. if ([addFileName isEqualToString:@""]) return serverUrl;
  113. if ([serverUrl isEqualToString:@"/"]) result = [serverUrl stringByAppendingString:addFileName];
  114. else result = [NSString stringWithFormat:@"%@/%@", serverUrl, addFileName];
  115. return result;
  116. }
  117. + (NSString *)createFileNameDate:(NSString *)fileName extension:(NSString *)extension
  118. {
  119. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  120. [formatter setDateFormat:@"yy-MM-dd HH-mm-ss"];
  121. NSString *fileNameDate = [formatter stringFromDate:[NSDate date]];
  122. NSString *returnFileName;
  123. if ([fileName isEqualToString:@""] && ![extension isEqualToString:@""]) {
  124. returnFileName = [NSString stringWithFormat:@"%@.%@", fileNameDate, extension];
  125. }
  126. if (![fileName isEqualToString:@""] && [extension isEqualToString:@""]) {
  127. returnFileName = [NSString stringWithFormat:@"%@ %@", fileName, fileNameDate];
  128. }
  129. if ([fileName isEqualToString:@""] && [extension isEqualToString:@""]) {
  130. returnFileName = fileNameDate;
  131. }
  132. if (![fileName isEqualToString:@""] && ![extension isEqualToString:@""]) {
  133. returnFileName = [NSString stringWithFormat:@"%@ %@.%@", fileName, fileNameDate, extension];
  134. }
  135. return returnFileName;
  136. }
  137. + (NSString *)createFileName:(NSString *)fileName fileDate:(NSDate *)fileDate fileType:(PHAssetMediaType)fileType keyFileName:(NSString *)keyFileName keyFileNameType:(NSString *)keyFileNameType keyFileNameOriginal:(NSString *)keyFileNameOriginal forcedNewFileName:(BOOL)forcedNewFileName
  138. {
  139. BOOL addFileNameType = NO;
  140. // Original FileName ?
  141. if ([[[NCKeychain alloc] init] getOriginalFileNameWithKey:keyFileNameOriginal] && !forcedNewFileName) {
  142. return fileName;
  143. }
  144. NSString *numberFileName;
  145. if ([fileName length] > 8) numberFileName = [fileName substringWithRange:NSMakeRange(04, 04)];
  146. else numberFileName = [NCKeychain alloc].incrementalNumber;
  147. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  148. [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  149. [formatter setDateFormat:@"yy-MM-dd HH-mm-ss"];
  150. NSString *fileNameDate = [formatter stringFromDate:fileDate];
  151. NSString *fileNameType = @"";
  152. if (fileType == PHAssetMediaTypeImage)
  153. fileNameType = NSLocalizedString(@"_photo_", nil);
  154. if (fileType == PHAssetMediaTypeVideo)
  155. fileNameType = NSLocalizedString(@"_video_", nil);
  156. if (fileType == PHAssetMediaTypeAudio)
  157. fileNameType = NSLocalizedString(@"_audio_", nil);
  158. if (fileType == PHAssetMediaTypeUnknown)
  159. fileNameType = NSLocalizedString(@"_unknown_", nil);
  160. // Use File Name Type
  161. if (keyFileNameType)
  162. addFileNameType = [[[NCKeychain alloc] init] getFileNameTypeWithKey:keyFileNameType];
  163. NSString *fileNameExt = [[fileName pathExtension] lowercaseString];
  164. if (keyFileName) {
  165. fileName = [[[NCKeychain alloc] init] getFileNameMaskWithKey:keyFileName];
  166. if ([fileName length] > 0) {
  167. [formatter setDateFormat:@"dd"];
  168. NSString *dayNumber = [formatter stringFromDate:fileDate];
  169. [formatter setDateFormat:@"MMM"];
  170. NSString *month = [formatter stringFromDate:fileDate];
  171. [formatter setDateFormat:@"MM"];
  172. NSString *monthNumber = [formatter stringFromDate:fileDate];
  173. [formatter setDateFormat:@"yyyy"];
  174. NSString *year = [formatter stringFromDate:fileDate];
  175. [formatter setDateFormat:@"yy"];
  176. NSString *yearNumber = [formatter stringFromDate:fileDate];
  177. [formatter setDateFormat:@"HH"];
  178. NSString *hour24 = [formatter stringFromDate:fileDate];
  179. [formatter setDateFormat:@"hh"];
  180. NSString *hour12 = [formatter stringFromDate:fileDate];
  181. [formatter setDateFormat:@"mm"];
  182. NSString *minute = [formatter stringFromDate:fileDate];
  183. [formatter setDateFormat:@"ss"];
  184. NSString *second = [formatter stringFromDate:fileDate];
  185. [formatter setDateFormat:@"a"];
  186. NSString *ampm = [formatter stringFromDate:fileDate];
  187. // Replace string with date
  188. fileName = [fileName stringByReplacingOccurrencesOfString:@"DD" withString:dayNumber];
  189. fileName = [fileName stringByReplacingOccurrencesOfString:@"MMM" withString:month];
  190. fileName = [fileName stringByReplacingOccurrencesOfString:@"MM" withString:monthNumber];
  191. fileName = [fileName stringByReplacingOccurrencesOfString:@"YYYY" withString:year];
  192. fileName = [fileName stringByReplacingOccurrencesOfString:@"YY" withString:yearNumber];
  193. fileName = [fileName stringByReplacingOccurrencesOfString:@"HH" withString:hour24];
  194. fileName = [fileName stringByReplacingOccurrencesOfString:@"hh" withString:hour12];
  195. fileName = [fileName stringByReplacingOccurrencesOfString:@"mm" withString:minute];
  196. fileName = [fileName stringByReplacingOccurrencesOfString:@"ss" withString:second];
  197. fileName = [fileName stringByReplacingOccurrencesOfString:@"ampm" withString:ampm];
  198. if (addFileNameType)
  199. fileName = [NSString stringWithFormat:@"%@%@%@.%@", fileNameType, fileName, numberFileName, fileNameExt];
  200. else
  201. fileName = [NSString stringWithFormat:@"%@%@.%@", fileName, numberFileName, fileNameExt];
  202. fileName = [fileName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  203. } else {
  204. if (addFileNameType)
  205. fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
  206. else
  207. fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
  208. }
  209. } else {
  210. if (addFileNameType)
  211. fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
  212. else
  213. fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
  214. }
  215. return fileName;
  216. }
  217. + (void)createDirectoryStandard
  218. {
  219. NSString *path;
  220. NSURL *dirGroup = [CCUtility getDirectoryGroup];
  221. NSLog(@"[LOG] Dir Group");
  222. NSLog(@"%@", [dirGroup path]);
  223. NSLog(@"[LOG] Program application ");
  224. NSLog(@"%@", [[CCUtility getDirectoryDocuments] stringByDeletingLastPathComponent]);
  225. // create Directory Documents
  226. path = [CCUtility getDirectoryDocuments];
  227. if (![[NSFileManager defaultManager] fileExistsAtPath: path])
  228. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  229. // create Directory audio => Library, Application Support, audio
  230. path = [CCUtility getDirectoryAudio];
  231. if (![[NSFileManager defaultManager] fileExistsAtPath: path])
  232. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  233. // create Directory database Nextcloud
  234. path = [[dirGroup URLByAppendingPathComponent:[[NCGlobal shared] appDatabaseNextcloud]] path];
  235. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  236. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  237. // create Directory User Data
  238. path = [[dirGroup URLByAppendingPathComponent:NCGlobal.shared.appUserData] path];
  239. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  240. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  241. // create Directory Provider Storage
  242. path = [CCUtility getDirectoryProviderStorage];
  243. if (![[NSFileManager defaultManager] fileExistsAtPath: path])
  244. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  245. // create Directory Scan
  246. path = [[dirGroup URLByAppendingPathComponent:NCGlobal.shared.appScan] path];
  247. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  248. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  249. // create Directory Temp
  250. path = NSTemporaryDirectory();
  251. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  252. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  253. // Directory Excluded From Backup
  254. [CCUtility addSkipBackupAttributeToItemAtURL:[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]];
  255. [CCUtility addSkipBackupAttributeToItemAtURL:[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.directoryProviderStorage]];
  256. [CCUtility addSkipBackupAttributeToItemAtURL:[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appApplicationSupport]];
  257. [CCUtility addSkipBackupAttributeToItemAtURL:[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appCertificates]];
  258. [CCUtility addSkipBackupAttributeToItemAtURL:[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appDatabaseNextcloud]];
  259. [CCUtility addSkipBackupAttributeToItemAtURL:[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appScan]];
  260. [CCUtility addSkipBackupAttributeToItemAtURL:[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appUserData]];
  261. }
  262. + (NSURL *)getDirectoryGroup
  263. {
  264. NSURL *path = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[NCBrandOptions shared].capabilitiesGroups];
  265. return path;
  266. }
  267. // Return the path of directory Documents -> NSDocumentDirectory
  268. + (NSString *)getDirectoryDocuments
  269. {
  270. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  271. return [paths objectAtIndex:0];
  272. }
  273. + (NSString *)getDirectoryReaderMetadata
  274. {
  275. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  276. return [NSString stringWithFormat:@"%@/Reader Metadata", [paths objectAtIndex:0]];
  277. }
  278. // Return the path of directory Audio
  279. + (NSString *)getDirectoryAudio
  280. {
  281. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  282. return [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], @"audio"];
  283. }
  284. // Return the path of directory Cetificates
  285. + (NSString *)getDirectoryCerificates
  286. {
  287. NSString *path = [[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appCertificates] path];
  288. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  289. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  290. return path;
  291. }
  292. + (NSString *)getDirectoryUserData
  293. {
  294. NSString *path = [[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appUserData] path];
  295. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  296. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  297. return path;
  298. }
  299. + (NSString *)getDirectoryProviderStorage
  300. {
  301. NSString *path = [[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.directoryProviderStorage] path];
  302. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  303. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  304. return path;
  305. }
  306. + (NSString *)getDirectoryProviderStorageOcId:(NSString *)ocId
  307. {
  308. NSString *path = [NSString stringWithFormat:@"%@/%@", [self getDirectoryProviderStorage], ocId];
  309. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  310. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  311. return path;
  312. }
  313. + (NSString *)getDirectoryProviderStorageOcId:(NSString *)ocId fileNameView:(NSString *)fileNameView
  314. {
  315. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@", [self getDirectoryProviderStorageOcId:ocId], fileNameView];
  316. // if do not exists create file 0 length
  317. // causes files with lenth 0 to never be downloaded, because already exist
  318. // also makes it impossible to delete any file with length 0 (from cache)
  319. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath] == NO) {
  320. [[NSFileManager defaultManager] createFileAtPath:fileNamePath contents:nil attributes:nil];
  321. }
  322. return fileNamePath;
  323. }
  324. + (NSString *)getDirectoryProviderStorageIconOcId:(NSString *)ocId etag:(NSString *)etag
  325. {
  326. return [NSString stringWithFormat:@"%@/%@.small.%@", [self getDirectoryProviderStorageOcId:ocId], etag, [NCGlobal shared].extensionPreview];
  327. }
  328. + (NSString *)getDirectoryProviderStoragePreviewOcId:(NSString *)ocId etag:(NSString *)etag
  329. {
  330. return [NSString stringWithFormat:@"%@/%@.preview.%@", [self getDirectoryProviderStorageOcId:ocId], etag, [NCGlobal shared].extensionPreview];
  331. }
  332. + (BOOL)fileProviderStorageExists:(tableMetadata *)metadata
  333. {
  334. NSString *fileNameViewPath = [self getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileNameView];
  335. NSString *fileNamePath = [self getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileName];
  336. unsigned long long fileNameViewSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:fileNameViewPath error:nil] fileSize];
  337. unsigned long long fileNameSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:fileNamePath error:nil] fileSize];
  338. if (metadata.isDirectoryE2EE == true) {
  339. if ((fileNameSize == metadata.size || fileNameViewSize == metadata.size) && fileNameViewSize > 0) {
  340. return true;
  341. } else {
  342. return false;
  343. }
  344. } else {
  345. return fileNameViewSize == metadata.size;
  346. }
  347. }
  348. + (int64_t)fileProviderStorageSize:(NSString *)ocId fileNameView:(NSString *)fileNameView
  349. {
  350. NSString *fileNamePath = [self getDirectoryProviderStorageOcId:ocId fileNameView:fileNameView];
  351. int64_t fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:fileNamePath error:nil] fileSize];
  352. return fileSize;
  353. }
  354. + (BOOL)fileProviderStoragePreviewIconExists:(NSString *)ocId etag:(NSString *)etag
  355. {
  356. NSString *fileNamePathPreview = [self getDirectoryProviderStoragePreviewOcId:ocId etag:etag];
  357. NSString *fileNamePathIcon = [self getDirectoryProviderStorageIconOcId:ocId etag:etag];
  358. unsigned long long fileSizePreview = [[[NSFileManager defaultManager] attributesOfItemAtPath:fileNamePathPreview error:nil] fileSize];
  359. unsigned long long fileSizeIcon = [[[NSFileManager defaultManager] attributesOfItemAtPath:fileNamePathIcon error:nil] fileSize];
  360. if (fileSizePreview > 0 && fileSizeIcon > 0) return true;
  361. else return false;
  362. }
  363. + (void)removeGroupApplicationSupport
  364. {
  365. NSURL *dirGroup = [CCUtility getDirectoryGroup];
  366. NSString *path = [[dirGroup URLByAppendingPathComponent:NCGlobal.shared.appApplicationSupport] path];
  367. [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
  368. }
  369. + (void)removeGroupLibraryDirectory
  370. {
  371. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryScan] error:nil];
  372. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryUserData] error:nil];
  373. }
  374. + (void)removeGroupDirectoryProviderStorage
  375. {
  376. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorage] error:nil];
  377. }
  378. + (void)removeDocumentsDirectory
  379. {
  380. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryDocuments] error:nil];
  381. }
  382. + (void)removeTemporaryDirectory
  383. {
  384. [[NSFileManager defaultManager] removeItemAtPath:NSTemporaryDirectory() error:nil];
  385. }
  386. + (void)emptyTemporaryDirectory
  387. {
  388. NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
  389. for (NSString *file in tmpDirectory) {
  390. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
  391. }
  392. }
  393. + (NSString *)getTitleSectionDate:(NSDate *)date
  394. {
  395. NSString *title;
  396. NSDate *today = [NSDate date];
  397. NSDate *yesterday = [today dateByAddingTimeInterval: -86400.0];
  398. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:[NSDate distantPast]]]) {
  399. title = NSLocalizedString(@"_no_date_", nil);
  400. } else {
  401. title = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterLongStyle timeStyle:0];
  402. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:today]])
  403. title = [NSString stringWithFormat:NSLocalizedString(@"_today_", nil)];
  404. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:yesterday]])
  405. title = [NSString stringWithFormat:NSLocalizedString(@"_yesterday_", nil)];
  406. }
  407. return title;
  408. }
  409. + (void)moveFileAtPath:(NSString *)atPath toPath:(NSString *)toPath
  410. {
  411. [[NSFileManager defaultManager] removeItemAtPath:toPath error:nil];
  412. [[NSFileManager defaultManager] copyItemAtPath:atPath toPath:toPath error:nil];
  413. [[NSFileManager defaultManager] removeItemAtPath:atPath error:nil];
  414. }
  415. + (void)copyFileAtPath:(NSString *)atPath toPath:(NSString *)toPath
  416. {
  417. [[NSFileManager defaultManager] removeItemAtPath:toPath error:nil];
  418. [[NSFileManager defaultManager] copyItemAtPath:atPath toPath:toPath error:nil];
  419. }
  420. + (void)removeFileAtPath:(NSString *)atPath
  421. {
  422. [[NSFileManager defaultManager] removeItemAtPath:atPath error:nil];
  423. }
  424. + (void)createDirectoryAtPath:(NSString *)atPath
  425. {
  426. [[NSFileManager defaultManager] createDirectoryAtPath:atPath withIntermediateDirectories:true attributes:nil error:nil];
  427. }
  428. + (NSString *)returnPathfromServerUrl:(NSString *)serverUrl urlBase:(NSString *)urlBase userId:(NSString *)userId account:(NSString *)account
  429. {
  430. NSString *homeServer = [[NCUtilityFileSystem shared] getHomeServerWithUrlBase:urlBase userId:userId];
  431. NSString *path = [serverUrl stringByReplacingOccurrencesOfString:homeServer withString:@""];
  432. return path;
  433. }
  434. + (NSString *)returnFileNamePathFromFileName:(NSString *)metadataFileName serverUrl:(NSString *)serverUrl urlBase:(NSString *)urlBase userId:(NSString *)userId account:(NSString *)account
  435. {
  436. if (metadataFileName == nil || serverUrl == nil || urlBase == nil) {
  437. return @"";
  438. }
  439. NSString *homeServer = [[NCUtilityFileSystem shared] getHomeServerWithUrlBase:urlBase userId:userId];
  440. NSString *fileName = [NSString stringWithFormat:@"%@/%@", [serverUrl stringByReplacingOccurrencesOfString:homeServer withString:@""], metadataFileName];
  441. if ([fileName hasPrefix:@"/"]) fileName = [fileName substringFromIndex:1];
  442. return fileName;
  443. }
  444. + (NSString *)getMimeType:(NSString *)fileNameView
  445. {
  446. CFStringRef fileUTI = nil;
  447. NSString *returnFileUTI = nil;
  448. if ([fileNameView isEqualToString:@"."]) {
  449. return returnFileUTI;
  450. } else {
  451. CFStringRef fileExtension = (__bridge CFStringRef)[fileNameView pathExtension];
  452. NSString *ext = (__bridge NSString *)fileExtension;
  453. ext = ext.uppercaseString;
  454. fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  455. if (fileUTI != nil) {
  456. returnFileUTI = (__bridge NSString *)fileUTI;
  457. CFRelease(fileUTI);
  458. }
  459. }
  460. return returnFileUTI;
  461. }
  462. + (NSString *)getDirectoryScan
  463. {
  464. NSString *path = [[[CCUtility getDirectoryGroup] URLByAppendingPathComponent:NCGlobal.shared.appScan] path];
  465. if (![[NSFileManager defaultManager] fileExistsAtPath:path])
  466. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  467. return path;
  468. }
  469. #pragma --------------------------------------------------------------------------------------------
  470. #pragma mark ===== Share Permissions =====
  471. #pragma --------------------------------------------------------------------------------------------
  472. + (NSInteger) getPermissionsValueByCanEdit:(BOOL)canEdit andCanCreate:(BOOL)canCreate andCanChange:(BOOL)canChange andCanDelete:(BOOL)canDelete andCanShare:(BOOL)canShare andIsFolder:(BOOL) isFolder
  473. {
  474. NSInteger permissionsValue = NCGlobal.shared.permissionReadShare;
  475. if (canEdit && !isFolder) {
  476. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  477. }
  478. if (canCreate & isFolder) {
  479. permissionsValue = permissionsValue + NCGlobal.shared.permissionCreateShare;
  480. }
  481. if (canChange && isFolder) {
  482. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  483. }
  484. if (canDelete & isFolder) {
  485. permissionsValue = permissionsValue + NCGlobal.shared.permissionDeleteShare;
  486. }
  487. if (canShare) {
  488. permissionsValue = permissionsValue + NCGlobal.shared.permissionShareShare;
  489. }
  490. return permissionsValue;
  491. }
  492. + (BOOL) isPermissionToCanCreate:(NSInteger) permissionValue {
  493. BOOL canCreate = ((permissionValue & NCGlobal.shared.permissionCreateShare) > 0);
  494. return canCreate;
  495. }
  496. + (BOOL) isPermissionToCanChange:(NSInteger) permissionValue {
  497. BOOL canChange = ((permissionValue & NCGlobal.shared.permissionUpdateShare) > 0);
  498. return canChange;
  499. }
  500. + (BOOL) isPermissionToCanDelete:(NSInteger) permissionValue {
  501. BOOL canDelete = ((permissionValue & NCGlobal.shared.permissionDeleteShare) > 0);
  502. return canDelete;
  503. }
  504. + (BOOL) isPermissionToCanShare:(NSInteger) permissionValue {
  505. BOOL canShare = ((permissionValue & NCGlobal.shared.permissionShareShare) > 0);
  506. return canShare;
  507. }
  508. + (BOOL) isAnyPermissionToEdit:(NSInteger) permissionValue {
  509. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  510. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  511. BOOL canDelete = [self isPermissionToCanDelete:permissionValue];
  512. BOOL canEdit = (canCreate || canChange || canDelete);
  513. return canEdit;
  514. }
  515. + (BOOL) isPermissionToRead:(NSInteger) permissionValue {
  516. BOOL canRead = ((permissionValue & NCGlobal.shared.permissionReadShare) > 0);
  517. return canRead;
  518. }
  519. + (BOOL) isPermissionToReadCreateUpdate:(NSInteger) permissionValue {
  520. BOOL canRead = [self isPermissionToRead:permissionValue];
  521. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  522. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  523. BOOL canEdit = (canCreate && canChange && canRead);
  524. return canEdit;
  525. }
  526. #pragma --------------------------------------------------------------------------------------------
  527. #pragma mark ===== Third parts =====
  528. #pragma --------------------------------------------------------------------------------------------
  529. + (NSString *)getExtension:(NSString*)fileName
  530. {
  531. NSMutableArray *fileNameArray =[[NSMutableArray alloc] initWithArray: [fileName componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]]];
  532. NSString *extension = [NSString stringWithFormat:@"%@",[fileNameArray lastObject]];
  533. extension = [extension uppercaseString];
  534. //If the file has a ZIP extension obtain the previous one for check if it is a .pages.zip / .numbers.zip / .key.zip extension
  535. if ([extension isEqualToString:@"ZIP"]) {
  536. [fileNameArray removeLastObject];
  537. NSString *secondExtension = [NSString stringWithFormat:@"%@",[fileNameArray lastObject]];
  538. secondExtension = [secondExtension uppercaseString];
  539. if ([secondExtension isEqualToString:@"PAGES"] || [secondExtension isEqualToString:@"NUMBERS"] || [secondExtension isEqualToString:@"KEY"]) {
  540. extension = [NSString stringWithFormat:@"%@.%@",secondExtension,extension];
  541. return extension;
  542. }
  543. }
  544. return extension;
  545. }
  546. + (NSDate *)datetimeWithOutTime:(NSDate *)datDate
  547. {
  548. if (datDate == nil) return nil;
  549. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:datDate];
  550. datDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
  551. return datDate;
  552. }
  553. + (NSString *)valueForKey:(NSString *)key fromQueryItems:(NSArray *)queryItems
  554. {
  555. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", key];
  556. NSURLQueryItem *queryItem = [[queryItems filteredArrayUsingPredicate:predicate] firstObject];
  557. return queryItem.value;
  558. }
  559. + (NSDate *)getATime:(const char *)path
  560. {
  561. struct stat st;
  562. stat(path, &st);
  563. time_t accessed = st.st_atime;
  564. NSDate *date = [NSDate dateWithTimeIntervalSince1970:accessed];
  565. return date;
  566. }
  567. @end