CCUtility.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 *)createFileName:(NSString *)fileName fileDate:(NSDate *)fileDate fileType:(PHAssetMediaType)fileType keyFileName:(NSString *)keyFileName keyFileNameType:(NSString *)keyFileNameType keyFileNameOriginal:(NSString *)keyFileNameOriginal forcedNewFileName:(BOOL)forcedNewFileName
  34. {
  35. BOOL addFileNameType = NO;
  36. // Original FileName ?
  37. if ([[[NCKeychain alloc] init] getOriginalFileNameWithKey:keyFileNameOriginal] && !forcedNewFileName) {
  38. return fileName;
  39. }
  40. NSString *numberFileName;
  41. if ([fileName length] > 8) numberFileName = [fileName substringWithRange:NSMakeRange(04, 04)];
  42. else numberFileName = [[NCKeychain alloc] init].incrementalNumber;
  43. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  44. [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  45. [formatter setDateFormat:@"yy-MM-dd HH-mm-ss"];
  46. NSString *fileNameDate = [formatter stringFromDate:fileDate];
  47. NSString *fileNameType = @"";
  48. if (fileType == PHAssetMediaTypeImage)
  49. fileNameType = NSLocalizedString(@"_photo_", nil);
  50. if (fileType == PHAssetMediaTypeVideo)
  51. fileNameType = NSLocalizedString(@"_video_", nil);
  52. if (fileType == PHAssetMediaTypeAudio)
  53. fileNameType = NSLocalizedString(@"_audio_", nil);
  54. if (fileType == PHAssetMediaTypeUnknown)
  55. fileNameType = NSLocalizedString(@"_unknown_", nil);
  56. // Use File Name Type
  57. if (keyFileNameType)
  58. addFileNameType = [[[NCKeychain alloc] init] getFileNameTypeWithKey:keyFileNameType];
  59. NSString *fileNameExt = [[fileName pathExtension] lowercaseString];
  60. if (keyFileName) {
  61. fileName = [[[NCKeychain alloc] init] getFileNameMaskWithKey:keyFileName];
  62. if ([fileName length] > 0) {
  63. [formatter setDateFormat:@"dd"];
  64. NSString *dayNumber = [formatter stringFromDate:fileDate];
  65. [formatter setDateFormat:@"MMM"];
  66. NSString *month = [formatter stringFromDate:fileDate];
  67. [formatter setDateFormat:@"MM"];
  68. NSString *monthNumber = [formatter stringFromDate:fileDate];
  69. [formatter setDateFormat:@"yyyy"];
  70. NSString *year = [formatter stringFromDate:fileDate];
  71. [formatter setDateFormat:@"yy"];
  72. NSString *yearNumber = [formatter stringFromDate:fileDate];
  73. [formatter setDateFormat:@"HH"];
  74. NSString *hour24 = [formatter stringFromDate:fileDate];
  75. [formatter setDateFormat:@"hh"];
  76. NSString *hour12 = [formatter stringFromDate:fileDate];
  77. [formatter setDateFormat:@"mm"];
  78. NSString *minute = [formatter stringFromDate:fileDate];
  79. [formatter setDateFormat:@"ss"];
  80. NSString *second = [formatter stringFromDate:fileDate];
  81. [formatter setDateFormat:@"a"];
  82. NSString *ampm = [formatter stringFromDate:fileDate];
  83. // Replace string with date
  84. fileName = [fileName stringByReplacingOccurrencesOfString:@"DD" withString:dayNumber];
  85. fileName = [fileName stringByReplacingOccurrencesOfString:@"MMM" withString:month];
  86. fileName = [fileName stringByReplacingOccurrencesOfString:@"MM" withString:monthNumber];
  87. fileName = [fileName stringByReplacingOccurrencesOfString:@"YYYY" withString:year];
  88. fileName = [fileName stringByReplacingOccurrencesOfString:@"YY" withString:yearNumber];
  89. fileName = [fileName stringByReplacingOccurrencesOfString:@"HH" withString:hour24];
  90. fileName = [fileName stringByReplacingOccurrencesOfString:@"hh" withString:hour12];
  91. fileName = [fileName stringByReplacingOccurrencesOfString:@"mm" withString:minute];
  92. fileName = [fileName stringByReplacingOccurrencesOfString:@"ss" withString:second];
  93. fileName = [fileName stringByReplacingOccurrencesOfString:@"ampm" withString:ampm];
  94. if (addFileNameType)
  95. fileName = [NSString stringWithFormat:@"%@%@%@.%@", fileNameType, fileName, numberFileName, fileNameExt];
  96. else
  97. fileName = [NSString stringWithFormat:@"%@%@.%@", fileName, numberFileName, fileNameExt];
  98. fileName = [fileName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  99. } else {
  100. if (addFileNameType)
  101. fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
  102. else
  103. fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
  104. }
  105. } else {
  106. if (addFileNameType)
  107. fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
  108. else
  109. fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
  110. }
  111. return fileName;
  112. }
  113. + (NSString *)getTitleSectionDate:(NSDate *)date
  114. {
  115. NSString *title;
  116. NSDate *today = [NSDate date];
  117. NSDate *yesterday = [today dateByAddingTimeInterval: -86400.0];
  118. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:[NSDate distantPast]]]) {
  119. title = NSLocalizedString(@"_no_date_", nil);
  120. } else {
  121. title = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterLongStyle timeStyle:0];
  122. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:today]])
  123. title = [NSString stringWithFormat:NSLocalizedString(@"_today_", nil)];
  124. if ([date isEqualToDate:[CCUtility datetimeWithOutTime:yesterday]])
  125. title = [NSString stringWithFormat:NSLocalizedString(@"_yesterday_", nil)];
  126. }
  127. return title;
  128. }
  129. + (NSString *)getMimeType:(NSString *)fileNameView
  130. {
  131. CFStringRef fileUTI = nil;
  132. NSString *returnFileUTI = nil;
  133. if ([fileNameView isEqualToString:@"."]) {
  134. return returnFileUTI;
  135. } else {
  136. CFStringRef fileExtension = (__bridge CFStringRef)[fileNameView pathExtension];
  137. NSString *ext = (__bridge NSString *)fileExtension;
  138. ext = ext.uppercaseString;
  139. fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  140. if (fileUTI != nil) {
  141. returnFileUTI = (__bridge NSString *)fileUTI;
  142. CFRelease(fileUTI);
  143. }
  144. }
  145. return returnFileUTI;
  146. }
  147. #pragma --------------------------------------------------------------------------------------------
  148. #pragma mark ===== Share Permissions =====
  149. #pragma --------------------------------------------------------------------------------------------
  150. + (NSInteger) getPermissionsValueByCanEdit:(BOOL)canEdit andCanCreate:(BOOL)canCreate andCanChange:(BOOL)canChange andCanDelete:(BOOL)canDelete andCanShare:(BOOL)canShare andIsFolder:(BOOL) isFolder
  151. {
  152. NSInteger permissionsValue = NCGlobal.shared.permissionReadShare;
  153. if (canEdit && !isFolder) {
  154. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  155. }
  156. if (canCreate & isFolder) {
  157. permissionsValue = permissionsValue + NCGlobal.shared.permissionCreateShare;
  158. }
  159. if (canChange && isFolder) {
  160. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  161. }
  162. if (canDelete & isFolder) {
  163. permissionsValue = permissionsValue + NCGlobal.shared.permissionDeleteShare;
  164. }
  165. if (canShare) {
  166. permissionsValue = permissionsValue + NCGlobal.shared.permissionShareShare;
  167. }
  168. return permissionsValue;
  169. }
  170. + (BOOL) isPermissionToCanCreate:(NSInteger) permissionValue {
  171. BOOL canCreate = ((permissionValue & NCGlobal.shared.permissionCreateShare) > 0);
  172. return canCreate;
  173. }
  174. + (BOOL) isPermissionToCanChange:(NSInteger) permissionValue {
  175. BOOL canChange = ((permissionValue & NCGlobal.shared.permissionUpdateShare) > 0);
  176. return canChange;
  177. }
  178. + (BOOL) isPermissionToCanDelete:(NSInteger) permissionValue {
  179. BOOL canDelete = ((permissionValue & NCGlobal.shared.permissionDeleteShare) > 0);
  180. return canDelete;
  181. }
  182. + (BOOL) isPermissionToCanShare:(NSInteger) permissionValue {
  183. BOOL canShare = ((permissionValue & NCGlobal.shared.permissionShareShare) > 0);
  184. return canShare;
  185. }
  186. + (BOOL) isAnyPermissionToEdit:(NSInteger) permissionValue {
  187. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  188. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  189. BOOL canDelete = [self isPermissionToCanDelete:permissionValue];
  190. BOOL canEdit = (canCreate || canChange || canDelete);
  191. return canEdit;
  192. }
  193. + (BOOL) isPermissionToRead:(NSInteger) permissionValue {
  194. BOOL canRead = ((permissionValue & NCGlobal.shared.permissionReadShare) > 0);
  195. return canRead;
  196. }
  197. + (BOOL) isPermissionToReadCreateUpdate:(NSInteger) permissionValue {
  198. BOOL canRead = [self isPermissionToRead:permissionValue];
  199. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  200. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  201. BOOL canEdit = (canCreate && canChange && canRead);
  202. return canEdit;
  203. }
  204. #pragma --------------------------------------------------------------------------------------------
  205. #pragma mark ===== Third parts =====
  206. #pragma --------------------------------------------------------------------------------------------
  207. + (NSDate *)datetimeWithOutTime:(NSDate *)datDate
  208. {
  209. if (datDate == nil) return nil;
  210. NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:datDate];
  211. datDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
  212. return datDate;
  213. }
  214. + (NSDate *)getATime:(const char *)path
  215. {
  216. struct stat st;
  217. stat(path, &st);
  218. time_t accessed = st.st_atime;
  219. NSDate *date = [NSDate dateWithTimeIntervalSince1970:accessed];
  220. return date;
  221. }
  222. @end