CCUtility.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 *)getMimeType:(NSString *)fileNameView
  114. {
  115. CFStringRef fileUTI = nil;
  116. NSString *returnFileUTI = nil;
  117. if ([fileNameView isEqualToString:@"."]) {
  118. return returnFileUTI;
  119. } else {
  120. CFStringRef fileExtension = (__bridge CFStringRef)[fileNameView pathExtension];
  121. NSString *ext = (__bridge NSString *)fileExtension;
  122. ext = ext.uppercaseString;
  123. fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  124. if (fileUTI != nil) {
  125. returnFileUTI = (__bridge NSString *)fileUTI;
  126. CFRelease(fileUTI);
  127. }
  128. }
  129. return returnFileUTI;
  130. }
  131. #pragma --------------------------------------------------------------------------------------------
  132. #pragma mark ===== Share Permissions =====
  133. #pragma --------------------------------------------------------------------------------------------
  134. + (NSInteger) getPermissionsValueByCanEdit:(BOOL)canEdit andCanCreate:(BOOL)canCreate andCanChange:(BOOL)canChange andCanDelete:(BOOL)canDelete andCanShare:(BOOL)canShare andIsFolder:(BOOL) isFolder
  135. {
  136. NSInteger permissionsValue = NCGlobal.shared.permissionReadShare;
  137. if (canEdit && !isFolder) {
  138. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  139. }
  140. if (canCreate & isFolder) {
  141. permissionsValue = permissionsValue + NCGlobal.shared.permissionCreateShare;
  142. }
  143. if (canChange && isFolder) {
  144. permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
  145. }
  146. if (canDelete & isFolder) {
  147. permissionsValue = permissionsValue + NCGlobal.shared.permissionDeleteShare;
  148. }
  149. if (canShare) {
  150. permissionsValue = permissionsValue + NCGlobal.shared.permissionShareShare;
  151. }
  152. return permissionsValue;
  153. }
  154. + (BOOL) isPermissionToCanCreate:(NSInteger) permissionValue {
  155. BOOL canCreate = ((permissionValue & NCGlobal.shared.permissionCreateShare) > 0);
  156. return canCreate;
  157. }
  158. + (BOOL) isPermissionToCanChange:(NSInteger) permissionValue {
  159. BOOL canChange = ((permissionValue & NCGlobal.shared.permissionUpdateShare) > 0);
  160. return canChange;
  161. }
  162. + (BOOL) isPermissionToCanDelete:(NSInteger) permissionValue {
  163. BOOL canDelete = ((permissionValue & NCGlobal.shared.permissionDeleteShare) > 0);
  164. return canDelete;
  165. }
  166. + (BOOL) isPermissionToCanShare:(NSInteger) permissionValue {
  167. BOOL canShare = ((permissionValue & NCGlobal.shared.permissionShareShare) > 0);
  168. return canShare;
  169. }
  170. + (BOOL) isAnyPermissionToEdit:(NSInteger) permissionValue {
  171. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  172. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  173. BOOL canDelete = [self isPermissionToCanDelete:permissionValue];
  174. BOOL canEdit = (canCreate || canChange || canDelete);
  175. return canEdit;
  176. }
  177. + (BOOL) isPermissionToRead:(NSInteger) permissionValue {
  178. BOOL canRead = ((permissionValue & NCGlobal.shared.permissionReadShare) > 0);
  179. return canRead;
  180. }
  181. + (BOOL) isPermissionToReadCreateUpdate:(NSInteger) permissionValue {
  182. BOOL canRead = [self isPermissionToRead:permissionValue];
  183. BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
  184. BOOL canChange = [self isPermissionToCanChange:permissionValue];
  185. BOOL canEdit = (canCreate && canChange && canRead);
  186. return canEdit;
  187. }
  188. #pragma --------------------------------------------------------------------------------------------
  189. #pragma mark ===== Third parts =====
  190. #pragma --------------------------------------------------------------------------------------------
  191. + (NSDate *)getATime:(const char *)path
  192. {
  193. struct stat st;
  194. stat(path, &st);
  195. time_t accessed = st.st_atime;
  196. NSDate *date = [NSDate dateWithTimeIntervalSince1970:accessed];
  197. return date;
  198. }
  199. @end