CCUtility.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 *)returnPathfromServerUrl:(NSString *)serverUrl urlBase:(NSString *)urlBase userId:(NSString *)userId account:(NSString *)account
  166. {
  167. NSString *homeServer = [[NCUtilityFileSystem shared] getHomeServerWithUrlBase:urlBase userId:userId];
  168. NSString *path = [serverUrl stringByReplacingOccurrencesOfString:homeServer withString:@""];
  169. return path;
  170. }
  171. + (NSString *)returnFileNamePathFromFileName:(NSString *)metadataFileName serverUrl:(NSString *)serverUrl urlBase:(NSString *)urlBase userId:(NSString *)userId account:(NSString *)account
  172. {
  173. if (metadataFileName == nil || serverUrl == nil || urlBase == nil) {
  174. return @"";
  175. }
  176. NSString *homeServer = [[NCUtilityFileSystem shared] getHomeServerWithUrlBase:urlBase userId:userId];
  177. NSString *fileName = [NSString stringWithFormat:@"%@/%@", [serverUrl stringByReplacingOccurrencesOfString:homeServer withString:@""], metadataFileName];
  178. if ([fileName hasPrefix:@"/"]) fileName = [fileName substringFromIndex:1];
  179. return fileName;
  180. }
  181. + (NSString *)getMimeType:(NSString *)fileNameView
  182. {
  183. CFStringRef fileUTI = nil;
  184. NSString *returnFileUTI = nil;
  185. if ([fileNameView isEqualToString:@"."]) {
  186. return returnFileUTI;
  187. } else {
  188. CFStringRef fileExtension = (__bridge CFStringRef)[fileNameView pathExtension];
  189. NSString *ext = (__bridge NSString *)fileExtension;
  190. ext = ext.uppercaseString;
  191. fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  192. if (fileUTI != nil) {
  193. returnFileUTI = (__bridge NSString *)fileUTI;
  194. CFRelease(fileUTI);
  195. }
  196. }
  197. return returnFileUTI;
  198. }
  199. #pragma --------------------------------------------------------------------------------------------
  200. #pragma mark ===== Share Permissions =====
  201. #pragma --------------------------------------------------------------------------------------------
  202. + (NSInteger) getPermissionsValueByCanEdit:(BOOL)canEdit andCanCreate:(BOOL)canCreate andCanChange:(BOOL)canChange andCanDelete:(BOOL)canDelete andCanShare:(BOOL)canShare andIsFolder:(BOOL) isFolder
  203. {
  204. NSInteger permissionsValue = NCGlobal.shared.permissionReadShare;
  205. if (canEdit && !isFolder) {
  206. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  207. }
  208. if (canCreate & isFolder) {
  209. permissionsValue = permissionsValue + NCGlobal.shared.permissionCreateShare;
  210. }
  211. if (canChange && isFolder) {
  212. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  213. }
  214. if (canDelete & isFolder) {
  215. permissionsValue = permissionsValue + NCGlobal.shared.permissionDeleteShare;
  216. }
  217. if (canShare) {
  218. permissionsValue = permissionsValue + NCGlobal.shared.permissionShareShare;
  219. }
  220. return permissionsValue;
  221. }
  222. + (BOOL) isPermissionToCanCreate:(NSInteger) permissionValue {
  223. BOOL canCreate = ((permissionValue & NCGlobal.shared.permissionCreateShare) > 0);
  224. return canCreate;
  225. }
  226. + (BOOL) isPermissionToCanChange:(NSInteger) permissionValue {
  227. BOOL canChange = ((permissionValue & NCGlobal.shared.permissionUpdateShare) > 0);
  228. return canChange;
  229. }
  230. + (BOOL) isPermissionToCanDelete:(NSInteger) permissionValue {
  231. BOOL canDelete = ((permissionValue & NCGlobal.shared.permissionDeleteShare) > 0);
  232. return canDelete;
  233. }
  234. + (BOOL) isPermissionToCanShare:(NSInteger) permissionValue {
  235. BOOL canShare = ((permissionValue & NCGlobal.shared.permissionShareShare) > 0);
  236. return canShare;
  237. }
  238. + (BOOL) isAnyPermissionToEdit:(NSInteger) permissionValue {
  239. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  240. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  241. BOOL canDelete = [self isPermissionToCanDelete:permissionValue];
  242. BOOL canEdit = (canCreate || canChange || canDelete);
  243. return canEdit;
  244. }
  245. + (BOOL) isPermissionToRead:(NSInteger) permissionValue {
  246. BOOL canRead = ((permissionValue & NCGlobal.shared.permissionReadShare) > 0);
  247. return canRead;
  248. }
  249. + (BOOL) isPermissionToReadCreateUpdate:(NSInteger) permissionValue {
  250. BOOL canRead = [self isPermissionToRead:permissionValue];
  251. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  252. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  253. BOOL canEdit = (canCreate && canChange && canRead);
  254. return canEdit;
  255. }
  256. #pragma --------------------------------------------------------------------------------------------
  257. #pragma mark ===== Third parts =====
  258. #pragma --------------------------------------------------------------------------------------------
  259. + (NSString *)getExtension:(NSString*)fileName
  260. {
  261. NSMutableArray *fileNameArray =[[NSMutableArray alloc] initWithArray: [fileName componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]]];
  262. NSString *extension = [NSString stringWithFormat:@"%@",[fileNameArray lastObject]];
  263. extension = [extension uppercaseString];
  264. //If the file has a ZIP extension obtain the previous one for check if it is a .pages.zip / .numbers.zip / .key.zip extension
  265. if ([extension isEqualToString:@"ZIP"]) {
  266. [fileNameArray removeLastObject];
  267. NSString *secondExtension = [NSString stringWithFormat:@"%@",[fileNameArray lastObject]];
  268. secondExtension = [secondExtension uppercaseString];
  269. if ([secondExtension isEqualToString:@"PAGES"] || [secondExtension isEqualToString:@"NUMBERS"] || [secondExtension isEqualToString:@"KEY"]) {
  270. extension = [NSString stringWithFormat:@"%@.%@",secondExtension,extension];
  271. return extension;
  272. }
  273. }
  274. return extension;
  275. }
  276. + (NSDate *)datetimeWithOutTime:(NSDate *)datDate
  277. {
  278. if (datDate == nil) return nil;
  279. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:datDate];
  280. datDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
  281. return datDate;
  282. }
  283. + (NSDate *)getATime:(const char *)path
  284. {
  285. struct stat st;
  286. stat(path, &st);
  287. time_t accessed = st.st_atime;
  288. NSDate *date = [NSDate dateWithTimeIntervalSince1970:accessed];
  289. return date;
  290. }
  291. @end