CCUtility.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. + (NSString *)dateDiff:(NSDate *) convertedDate
  39. {
  40. NSDate *todayDate = [NSDate date];
  41. double ti = [convertedDate timeIntervalSinceDate:todayDate];
  42. ti = ti * -1;
  43. if (ti < 60) {
  44. return NSLocalizedString(@"_less_a_minute_", nil);
  45. } else if (ti < 3600) {
  46. int diff = round(ti / 60);
  47. if (diff == 1) {
  48. return NSLocalizedString(@"_a_minute_ago_", nil);
  49. } else {
  50. return [NSString stringWithFormat:NSLocalizedString(@"_minutes_ago_", nil), diff];
  51. }
  52. } else if (ti < 86400) {
  53. int diff = round(ti / 60 / 60);
  54. if (diff == 1) {
  55. return NSLocalizedString(@"_an_hour_ago_", nil);
  56. } else {
  57. return[NSString stringWithFormat:NSLocalizedString(@"_hours_ago_", nil), diff];
  58. }
  59. } else if (ti < 86400 * 30) {
  60. int diff = round(ti / 60 / 60 / 24);
  61. if (diff == 1) {
  62. return NSLocalizedString(@"_a_day_ago_", nil);
  63. } else {
  64. return[NSString stringWithFormat:NSLocalizedString(@"_days_ago_", nil), diff];
  65. }
  66. } else {
  67. // Older than one month
  68. NSDateFormatter *df = [[NSDateFormatter alloc] init];
  69. [df setFormatterBehavior:NSDateFormatterBehavior10_4];
  70. [df setDateStyle:NSDateFormatterMediumStyle];
  71. return [df stringFromDate:convertedDate];
  72. }
  73. }
  74. + (NSString *)transformedSize:(int64_t)value
  75. {
  76. NSString *string = [NSByteCountFormatter stringFromByteCount:value countStyle:NSByteCountFormatterCountStyleBinary];
  77. return string;
  78. }
  79. // Remove do not forbidden characters for Nextcloud Server
  80. + (NSString *)removeForbiddenCharactersServer:(NSString *)fileName
  81. {
  82. NSArray *arrayForbiddenCharacters = [NSArray arrayWithObjects:@"/", nil];
  83. for (NSString *currentCharacter in arrayForbiddenCharacters) {
  84. fileName = [fileName stringByReplacingOccurrencesOfString:currentCharacter withString:@""];
  85. }
  86. return fileName;
  87. }
  88. + (NSString*)stringAppendServerUrl:(NSString *)serverUrl addFileName:(NSString *)addFileName
  89. {
  90. NSString *result;
  91. if (serverUrl == nil || addFileName == nil) return nil;
  92. if ([addFileName isEqualToString:@""]) return serverUrl;
  93. if ([serverUrl isEqualToString:@"/"]) result = [serverUrl stringByAppendingString:addFileName];
  94. else result = [NSString stringWithFormat:@"%@/%@", serverUrl, addFileName];
  95. return result;
  96. }
  97. + (NSString *)createFileNameDate:(NSString *)fileName extension:(NSString *)extension
  98. {
  99. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  100. [formatter setDateFormat:@"yy-MM-dd HH-mm-ss"];
  101. NSString *fileNameDate = [formatter stringFromDate:[NSDate date]];
  102. NSString *returnFileName;
  103. if ([fileName isEqualToString:@""] && ![extension isEqualToString:@""]) {
  104. returnFileName = [NSString stringWithFormat:@"%@.%@", fileNameDate, extension];
  105. }
  106. if (![fileName isEqualToString:@""] && [extension isEqualToString:@""]) {
  107. returnFileName = [NSString stringWithFormat:@"%@ %@", fileName, fileNameDate];
  108. }
  109. if ([fileName isEqualToString:@""] && [extension isEqualToString:@""]) {
  110. returnFileName = fileNameDate;
  111. }
  112. if (![fileName isEqualToString:@""] && ![extension isEqualToString:@""]) {
  113. returnFileName = [NSString stringWithFormat:@"%@ %@.%@", fileName, fileNameDate, extension];
  114. }
  115. return returnFileName;
  116. }
  117. + (NSString *)createFileName:(NSString *)fileName fileDate:(NSDate *)fileDate fileType:(PHAssetMediaType)fileType keyFileName:(NSString *)keyFileName keyFileNameType:(NSString *)keyFileNameType keyFileNameOriginal:(NSString *)keyFileNameOriginal forcedNewFileName:(BOOL)forcedNewFileName
  118. {
  119. BOOL addFileNameType = NO;
  120. // Original FileName ?
  121. if ([[[NCKeychain alloc] init] getOriginalFileNameWithKey:keyFileNameOriginal] && !forcedNewFileName) {
  122. return fileName;
  123. }
  124. NSString *numberFileName;
  125. if ([fileName length] > 8) numberFileName = [fileName substringWithRange:NSMakeRange(04, 04)];
  126. else numberFileName = [NCKeychain alloc].incrementalNumber;
  127. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  128. [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  129. [formatter setDateFormat:@"yy-MM-dd HH-mm-ss"];
  130. NSString *fileNameDate = [formatter stringFromDate:fileDate];
  131. NSString *fileNameType = @"";
  132. if (fileType == PHAssetMediaTypeImage)
  133. fileNameType = NSLocalizedString(@"_photo_", nil);
  134. if (fileType == PHAssetMediaTypeVideo)
  135. fileNameType = NSLocalizedString(@"_video_", nil);
  136. if (fileType == PHAssetMediaTypeAudio)
  137. fileNameType = NSLocalizedString(@"_audio_", nil);
  138. if (fileType == PHAssetMediaTypeUnknown)
  139. fileNameType = NSLocalizedString(@"_unknown_", nil);
  140. // Use File Name Type
  141. if (keyFileNameType)
  142. addFileNameType = [[[NCKeychain alloc] init] getFileNameTypeWithKey:keyFileNameType];
  143. NSString *fileNameExt = [[fileName pathExtension] lowercaseString];
  144. if (keyFileName) {
  145. fileName = [[[NCKeychain alloc] init] getFileNameMaskWithKey:keyFileName];
  146. if ([fileName length] > 0) {
  147. [formatter setDateFormat:@"dd"];
  148. NSString *dayNumber = [formatter stringFromDate:fileDate];
  149. [formatter setDateFormat:@"MMM"];
  150. NSString *month = [formatter stringFromDate:fileDate];
  151. [formatter setDateFormat:@"MM"];
  152. NSString *monthNumber = [formatter stringFromDate:fileDate];
  153. [formatter setDateFormat:@"yyyy"];
  154. NSString *year = [formatter stringFromDate:fileDate];
  155. [formatter setDateFormat:@"yy"];
  156. NSString *yearNumber = [formatter stringFromDate:fileDate];
  157. [formatter setDateFormat:@"HH"];
  158. NSString *hour24 = [formatter stringFromDate:fileDate];
  159. [formatter setDateFormat:@"hh"];
  160. NSString *hour12 = [formatter stringFromDate:fileDate];
  161. [formatter setDateFormat:@"mm"];
  162. NSString *minute = [formatter stringFromDate:fileDate];
  163. [formatter setDateFormat:@"ss"];
  164. NSString *second = [formatter stringFromDate:fileDate];
  165. [formatter setDateFormat:@"a"];
  166. NSString *ampm = [formatter stringFromDate:fileDate];
  167. // Replace string with date
  168. fileName = [fileName stringByReplacingOccurrencesOfString:@"DD" withString:dayNumber];
  169. fileName = [fileName stringByReplacingOccurrencesOfString:@"MMM" withString:month];
  170. fileName = [fileName stringByReplacingOccurrencesOfString:@"MM" withString:monthNumber];
  171. fileName = [fileName stringByReplacingOccurrencesOfString:@"YYYY" withString:year];
  172. fileName = [fileName stringByReplacingOccurrencesOfString:@"YY" withString:yearNumber];
  173. fileName = [fileName stringByReplacingOccurrencesOfString:@"HH" withString:hour24];
  174. fileName = [fileName stringByReplacingOccurrencesOfString:@"hh" withString:hour12];
  175. fileName = [fileName stringByReplacingOccurrencesOfString:@"mm" withString:minute];
  176. fileName = [fileName stringByReplacingOccurrencesOfString:@"ss" withString:second];
  177. fileName = [fileName stringByReplacingOccurrencesOfString:@"ampm" withString:ampm];
  178. if (addFileNameType)
  179. fileName = [NSString stringWithFormat:@"%@%@%@.%@", fileNameType, fileName, numberFileName, fileNameExt];
  180. else
  181. fileName = [NSString stringWithFormat:@"%@%@.%@", fileName, numberFileName, fileNameExt];
  182. fileName = [fileName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  183. } else {
  184. if (addFileNameType)
  185. fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
  186. else
  187. fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
  188. }
  189. } else {
  190. if (addFileNameType)
  191. fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
  192. else
  193. fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
  194. }
  195. return fileName;
  196. }
  197. + (NSString *)getTitleSectionDate:(NSDate *)date
  198. {
  199. NSString *title;
  200. NSDate *today = [NSDate date];
  201. NSDate *yesterday = [today dateByAddingTimeInterval: -86400.0];
  202. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:[NSDate distantPast]]]) {
  203. title = NSLocalizedString(@"_no_date_", nil);
  204. } else {
  205. title = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterLongStyle timeStyle:0];
  206. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:today]])
  207. title = [NSString stringWithFormat:NSLocalizedString(@"_today_", nil)];
  208. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:yesterday]])
  209. title = [NSString stringWithFormat:NSLocalizedString(@"_yesterday_", nil)];
  210. }
  211. return title;
  212. }
  213. + (void)moveFileAtPath:(NSString *)atPath toPath:(NSString *)toPath
  214. {
  215. [[NSFileManager defaultManager] removeItemAtPath:toPath error:nil];
  216. [[NSFileManager defaultManager] copyItemAtPath:atPath toPath:toPath error:nil];
  217. [[NSFileManager defaultManager] removeItemAtPath:atPath error:nil];
  218. }
  219. + (void)copyFileAtPath:(NSString *)atPath toPath:(NSString *)toPath
  220. {
  221. [[NSFileManager defaultManager] removeItemAtPath:toPath error:nil];
  222. [[NSFileManager defaultManager] copyItemAtPath:atPath toPath:toPath error:nil];
  223. }
  224. + (void)removeFileAtPath:(NSString *)atPath
  225. {
  226. [[NSFileManager defaultManager] removeItemAtPath:atPath error:nil];
  227. }
  228. + (void)createDirectoryAtPath:(NSString *)atPath
  229. {
  230. [[NSFileManager defaultManager] createDirectoryAtPath:atPath withIntermediateDirectories:true attributes:nil error:nil];
  231. }
  232. + (NSString *)returnPathfromServerUrl:(NSString *)serverUrl urlBase:(NSString *)urlBase userId:(NSString *)userId account:(NSString *)account
  233. {
  234. NSString *homeServer = [[NCUtilityFileSystem shared] getHomeServerWithUrlBase:urlBase userId:userId];
  235. NSString *path = [serverUrl stringByReplacingOccurrencesOfString:homeServer withString:@""];
  236. return path;
  237. }
  238. + (NSString *)returnFileNamePathFromFileName:(NSString *)metadataFileName serverUrl:(NSString *)serverUrl urlBase:(NSString *)urlBase userId:(NSString *)userId account:(NSString *)account
  239. {
  240. if (metadataFileName == nil || serverUrl == nil || urlBase == nil) {
  241. return @"";
  242. }
  243. NSString *homeServer = [[NCUtilityFileSystem shared] getHomeServerWithUrlBase:urlBase userId:userId];
  244. NSString *fileName = [NSString stringWithFormat:@"%@/%@", [serverUrl stringByReplacingOccurrencesOfString:homeServer withString:@""], metadataFileName];
  245. if ([fileName hasPrefix:@"/"]) fileName = [fileName substringFromIndex:1];
  246. return fileName;
  247. }
  248. + (NSString *)getMimeType:(NSString *)fileNameView
  249. {
  250. CFStringRef fileUTI = nil;
  251. NSString *returnFileUTI = nil;
  252. if ([fileNameView isEqualToString:@"."]) {
  253. return returnFileUTI;
  254. } else {
  255. CFStringRef fileExtension = (__bridge CFStringRef)[fileNameView pathExtension];
  256. NSString *ext = (__bridge NSString *)fileExtension;
  257. ext = ext.uppercaseString;
  258. fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  259. if (fileUTI != nil) {
  260. returnFileUTI = (__bridge NSString *)fileUTI;
  261. CFRelease(fileUTI);
  262. }
  263. }
  264. return returnFileUTI;
  265. }
  266. #pragma --------------------------------------------------------------------------------------------
  267. #pragma mark ===== Share Permissions =====
  268. #pragma --------------------------------------------------------------------------------------------
  269. + (NSInteger) getPermissionsValueByCanEdit:(BOOL)canEdit andCanCreate:(BOOL)canCreate andCanChange:(BOOL)canChange andCanDelete:(BOOL)canDelete andCanShare:(BOOL)canShare andIsFolder:(BOOL) isFolder
  270. {
  271. NSInteger permissionsValue = NCGlobal.shared.permissionReadShare;
  272. if (canEdit && !isFolder) {
  273. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  274. }
  275. if (canCreate & isFolder) {
  276. permissionsValue = permissionsValue + NCGlobal.shared.permissionCreateShare;
  277. }
  278. if (canChange && isFolder) {
  279. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  280. }
  281. if (canDelete & isFolder) {
  282. permissionsValue = permissionsValue + NCGlobal.shared.permissionDeleteShare;
  283. }
  284. if (canShare) {
  285. permissionsValue = permissionsValue + NCGlobal.shared.permissionShareShare;
  286. }
  287. return permissionsValue;
  288. }
  289. + (BOOL) isPermissionToCanCreate:(NSInteger) permissionValue {
  290. BOOL canCreate = ((permissionValue & NCGlobal.shared.permissionCreateShare) > 0);
  291. return canCreate;
  292. }
  293. + (BOOL) isPermissionToCanChange:(NSInteger) permissionValue {
  294. BOOL canChange = ((permissionValue & NCGlobal.shared.permissionUpdateShare) > 0);
  295. return canChange;
  296. }
  297. + (BOOL) isPermissionToCanDelete:(NSInteger) permissionValue {
  298. BOOL canDelete = ((permissionValue & NCGlobal.shared.permissionDeleteShare) > 0);
  299. return canDelete;
  300. }
  301. + (BOOL) isPermissionToCanShare:(NSInteger) permissionValue {
  302. BOOL canShare = ((permissionValue & NCGlobal.shared.permissionShareShare) > 0);
  303. return canShare;
  304. }
  305. + (BOOL) isAnyPermissionToEdit:(NSInteger) permissionValue {
  306. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  307. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  308. BOOL canDelete = [self isPermissionToCanDelete:permissionValue];
  309. BOOL canEdit = (canCreate || canChange || canDelete);
  310. return canEdit;
  311. }
  312. + (BOOL) isPermissionToRead:(NSInteger) permissionValue {
  313. BOOL canRead = ((permissionValue & NCGlobal.shared.permissionReadShare) > 0);
  314. return canRead;
  315. }
  316. + (BOOL) isPermissionToReadCreateUpdate:(NSInteger) permissionValue {
  317. BOOL canRead = [self isPermissionToRead:permissionValue];
  318. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  319. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  320. BOOL canEdit = (canCreate && canChange && canRead);
  321. return canEdit;
  322. }
  323. #pragma --------------------------------------------------------------------------------------------
  324. #pragma mark ===== Third parts =====
  325. #pragma --------------------------------------------------------------------------------------------
  326. + (NSString *)getExtension:(NSString*)fileName
  327. {
  328. NSMutableArray *fileNameArray =[[NSMutableArray alloc] initWithArray: [fileName componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]]];
  329. NSString *extension = [NSString stringWithFormat:@"%@",[fileNameArray lastObject]];
  330. extension = [extension uppercaseString];
  331. //If the file has a ZIP extension obtain the previous one for check if it is a .pages.zip / .numbers.zip / .key.zip extension
  332. if ([extension isEqualToString:@"ZIP"]) {
  333. [fileNameArray removeLastObject];
  334. NSString *secondExtension = [NSString stringWithFormat:@"%@",[fileNameArray lastObject]];
  335. secondExtension = [secondExtension uppercaseString];
  336. if ([secondExtension isEqualToString:@"PAGES"] || [secondExtension isEqualToString:@"NUMBERS"] || [secondExtension isEqualToString:@"KEY"]) {
  337. extension = [NSString stringWithFormat:@"%@.%@",secondExtension,extension];
  338. return extension;
  339. }
  340. }
  341. return extension;
  342. }
  343. + (NSDate *)datetimeWithOutTime:(NSDate *)datDate
  344. {
  345. if (datDate == nil) return nil;
  346. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:datDate];
  347. datDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
  348. return datDate;
  349. }
  350. + (NSString *)valueForKey:(NSString *)key fromQueryItems:(NSArray *)queryItems
  351. {
  352. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", key];
  353. NSURLQueryItem *queryItem = [[queryItems filteredArrayUsingPredicate:predicate] firstObject];
  354. return queryItem.value;
  355. }
  356. + (NSDate *)getATime:(const char *)path
  357. {
  358. struct stat st;
  359. stat(path, &st);
  360. time_t accessed = st.st_atime;
  361. NSDate *date = [NSDate dateWithTimeIntervalSince1970:accessed];
  362. return date;
  363. }
  364. @end