CCUtility.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 *)createFileName:(NSString *)fileName fileDate:(NSDate *)fileDate fileType:(PHAssetMediaType)fileType keyFileName:(NSString *)keyFileName keyFileNameType:(NSString *)keyFileNameType keyFileNameOriginal:(NSString *)keyFileNameOriginal forcedNewFileName:(BOOL)forcedNewFileName
  70. {
  71. BOOL addFileNameType = NO;
  72. // Original FileName ?
  73. if ([[[NCKeychain alloc] init] getOriginalFileNameWithKey:keyFileNameOriginal] && !forcedNewFileName) {
  74. return fileName;
  75. }
  76. NSString *numberFileName;
  77. if ([fileName length] > 8) numberFileName = [fileName substringWithRange:NSMakeRange(04, 04)];
  78. else numberFileName = [[NCKeychain alloc] init].incrementalNumber;
  79. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  80. [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  81. [formatter setDateFormat:@"yy-MM-dd HH-mm-ss"];
  82. NSString *fileNameDate = [formatter stringFromDate:fileDate];
  83. NSString *fileNameType = @"";
  84. if (fileType == PHAssetMediaTypeImage)
  85. fileNameType = NSLocalizedString(@"_photo_", nil);
  86. if (fileType == PHAssetMediaTypeVideo)
  87. fileNameType = NSLocalizedString(@"_video_", nil);
  88. if (fileType == PHAssetMediaTypeAudio)
  89. fileNameType = NSLocalizedString(@"_audio_", nil);
  90. if (fileType == PHAssetMediaTypeUnknown)
  91. fileNameType = NSLocalizedString(@"_unknown_", nil);
  92. // Use File Name Type
  93. if (keyFileNameType)
  94. addFileNameType = [[[NCKeychain alloc] init] getFileNameTypeWithKey:keyFileNameType];
  95. NSString *fileNameExt = [[fileName pathExtension] lowercaseString];
  96. if (keyFileName) {
  97. fileName = [[[NCKeychain alloc] init] getFileNameMaskWithKey:keyFileName];
  98. if ([fileName length] > 0) {
  99. [formatter setDateFormat:@"dd"];
  100. NSString *dayNumber = [formatter stringFromDate:fileDate];
  101. [formatter setDateFormat:@"MMM"];
  102. NSString *month = [formatter stringFromDate:fileDate];
  103. [formatter setDateFormat:@"MM"];
  104. NSString *monthNumber = [formatter stringFromDate:fileDate];
  105. [formatter setDateFormat:@"yyyy"];
  106. NSString *year = [formatter stringFromDate:fileDate];
  107. [formatter setDateFormat:@"yy"];
  108. NSString *yearNumber = [formatter stringFromDate:fileDate];
  109. [formatter setDateFormat:@"HH"];
  110. NSString *hour24 = [formatter stringFromDate:fileDate];
  111. [formatter setDateFormat:@"hh"];
  112. NSString *hour12 = [formatter stringFromDate:fileDate];
  113. [formatter setDateFormat:@"mm"];
  114. NSString *minute = [formatter stringFromDate:fileDate];
  115. [formatter setDateFormat:@"ss"];
  116. NSString *second = [formatter stringFromDate:fileDate];
  117. [formatter setDateFormat:@"a"];
  118. NSString *ampm = [formatter stringFromDate:fileDate];
  119. // Replace string with date
  120. fileName = [fileName stringByReplacingOccurrencesOfString:@"DD" withString:dayNumber];
  121. fileName = [fileName stringByReplacingOccurrencesOfString:@"MMM" withString:month];
  122. fileName = [fileName stringByReplacingOccurrencesOfString:@"MM" withString:monthNumber];
  123. fileName = [fileName stringByReplacingOccurrencesOfString:@"YYYY" withString:year];
  124. fileName = [fileName stringByReplacingOccurrencesOfString:@"YY" withString:yearNumber];
  125. fileName = [fileName stringByReplacingOccurrencesOfString:@"HH" withString:hour24];
  126. fileName = [fileName stringByReplacingOccurrencesOfString:@"hh" withString:hour12];
  127. fileName = [fileName stringByReplacingOccurrencesOfString:@"mm" withString:minute];
  128. fileName = [fileName stringByReplacingOccurrencesOfString:@"ss" withString:second];
  129. fileName = [fileName stringByReplacingOccurrencesOfString:@"ampm" withString:ampm];
  130. if (addFileNameType)
  131. fileName = [NSString stringWithFormat:@"%@%@%@.%@", fileNameType, fileName, numberFileName, fileNameExt];
  132. else
  133. fileName = [NSString stringWithFormat:@"%@%@.%@", fileName, numberFileName, fileNameExt];
  134. fileName = [fileName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  135. } else {
  136. if (addFileNameType)
  137. fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
  138. else
  139. fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
  140. }
  141. } else {
  142. if (addFileNameType)
  143. fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
  144. else
  145. fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
  146. }
  147. return fileName;
  148. }
  149. + (NSString *)getTitleSectionDate:(NSDate *)date
  150. {
  151. NSString *title;
  152. NSDate *today = [NSDate date];
  153. NSDate *yesterday = [today dateByAddingTimeInterval: -86400.0];
  154. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:[NSDate distantPast]]]) {
  155. title = NSLocalizedString(@"_no_date_", nil);
  156. } else {
  157. title = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterLongStyle timeStyle:0];
  158. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:today]])
  159. title = [NSString stringWithFormat:NSLocalizedString(@"_today_", nil)];
  160. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:yesterday]])
  161. title = [NSString stringWithFormat:NSLocalizedString(@"_yesterday_", nil)];
  162. }
  163. return title;
  164. }
  165. + (NSString *)returnFileNamePathFromFileName:(NSString *)metadataFileName serverUrl:(NSString *)serverUrl urlBase:(NSString *)urlBase userId:(NSString *)userId account:(NSString *)account
  166. {
  167. if (metadataFileName == nil || serverUrl == nil || urlBase == nil) {
  168. return @"";
  169. }
  170. NSString *homeServer = [[NCUtilityFileSystem shared] getHomeServerWithUrlBase:urlBase userId:userId];
  171. NSString *fileName = [NSString stringWithFormat:@"%@/%@", [serverUrl stringByReplacingOccurrencesOfString:homeServer withString:@""], metadataFileName];
  172. if ([fileName hasPrefix:@"/"]) fileName = [fileName substringFromIndex:1];
  173. return fileName;
  174. }
  175. + (NSString *)getMimeType:(NSString *)fileNameView
  176. {
  177. CFStringRef fileUTI = nil;
  178. NSString *returnFileUTI = nil;
  179. if ([fileNameView isEqualToString:@"."]) {
  180. return returnFileUTI;
  181. } else {
  182. CFStringRef fileExtension = (__bridge CFStringRef)[fileNameView pathExtension];
  183. NSString *ext = (__bridge NSString *)fileExtension;
  184. ext = ext.uppercaseString;
  185. fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  186. if (fileUTI != nil) {
  187. returnFileUTI = (__bridge NSString *)fileUTI;
  188. CFRelease(fileUTI);
  189. }
  190. }
  191. return returnFileUTI;
  192. }
  193. #pragma --------------------------------------------------------------------------------------------
  194. #pragma mark ===== Share Permissions =====
  195. #pragma --------------------------------------------------------------------------------------------
  196. + (NSInteger) getPermissionsValueByCanEdit:(BOOL)canEdit andCanCreate:(BOOL)canCreate andCanChange:(BOOL)canChange andCanDelete:(BOOL)canDelete andCanShare:(BOOL)canShare andIsFolder:(BOOL) isFolder
  197. {
  198. NSInteger permissionsValue = NCGlobal.shared.permissionReadShare;
  199. if (canEdit && !isFolder) {
  200. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  201. }
  202. if (canCreate & isFolder) {
  203. permissionsValue = permissionsValue + NCGlobal.shared.permissionCreateShare;
  204. }
  205. if (canChange && isFolder) {
  206. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  207. }
  208. if (canDelete & isFolder) {
  209. permissionsValue = permissionsValue + NCGlobal.shared.permissionDeleteShare;
  210. }
  211. if (canShare) {
  212. permissionsValue = permissionsValue + NCGlobal.shared.permissionShareShare;
  213. }
  214. return permissionsValue;
  215. }
  216. + (BOOL) isPermissionToCanCreate:(NSInteger) permissionValue {
  217. BOOL canCreate = ((permissionValue & NCGlobal.shared.permissionCreateShare) > 0);
  218. return canCreate;
  219. }
  220. + (BOOL) isPermissionToCanChange:(NSInteger) permissionValue {
  221. BOOL canChange = ((permissionValue & NCGlobal.shared.permissionUpdateShare) > 0);
  222. return canChange;
  223. }
  224. + (BOOL) isPermissionToCanDelete:(NSInteger) permissionValue {
  225. BOOL canDelete = ((permissionValue & NCGlobal.shared.permissionDeleteShare) > 0);
  226. return canDelete;
  227. }
  228. + (BOOL) isPermissionToCanShare:(NSInteger) permissionValue {
  229. BOOL canShare = ((permissionValue & NCGlobal.shared.permissionShareShare) > 0);
  230. return canShare;
  231. }
  232. + (BOOL) isAnyPermissionToEdit:(NSInteger) permissionValue {
  233. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  234. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  235. BOOL canDelete = [self isPermissionToCanDelete:permissionValue];
  236. BOOL canEdit = (canCreate || canChange || canDelete);
  237. return canEdit;
  238. }
  239. + (BOOL) isPermissionToRead:(NSInteger) permissionValue {
  240. BOOL canRead = ((permissionValue & NCGlobal.shared.permissionReadShare) > 0);
  241. return canRead;
  242. }
  243. + (BOOL) isPermissionToReadCreateUpdate:(NSInteger) permissionValue {
  244. BOOL canRead = [self isPermissionToRead:permissionValue];
  245. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  246. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  247. BOOL canEdit = (canCreate && canChange && canRead);
  248. return canEdit;
  249. }
  250. #pragma --------------------------------------------------------------------------------------------
  251. #pragma mark ===== Third parts =====
  252. #pragma --------------------------------------------------------------------------------------------
  253. + (NSString *)getExtension:(NSString*)fileName
  254. {
  255. NSMutableArray *fileNameArray =[[NSMutableArray alloc] initWithArray: [fileName componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]]];
  256. NSString *extension = [NSString stringWithFormat:@"%@",[fileNameArray lastObject]];
  257. extension = [extension uppercaseString];
  258. //If the file has a ZIP extension obtain the previous one for check if it is a .pages.zip / .numbers.zip / .key.zip extension
  259. if ([extension isEqualToString:@"ZIP"]) {
  260. [fileNameArray removeLastObject];
  261. NSString *secondExtension = [NSString stringWithFormat:@"%@",[fileNameArray lastObject]];
  262. secondExtension = [secondExtension uppercaseString];
  263. if ([secondExtension isEqualToString:@"PAGES"] || [secondExtension isEqualToString:@"NUMBERS"] || [secondExtension isEqualToString:@"KEY"]) {
  264. extension = [NSString stringWithFormat:@"%@.%@",secondExtension,extension];
  265. return extension;
  266. }
  267. }
  268. return extension;
  269. }
  270. + (NSDate *)datetimeWithOutTime:(NSDate *)datDate
  271. {
  272. if (datDate == nil) return nil;
  273. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:datDate];
  274. datDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
  275. return datDate;
  276. }
  277. + (NSDate *)getATime:(const char *)path
  278. {
  279. struct stat st;
  280. stat(path, &st);
  281. time_t accessed = st.st_atime;
  282. NSDate *date = [NSDate dateWithTimeIntervalSince1970:accessed];
  283. return date;
  284. }
  285. @end