CCUtility.m 16 KB

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