CCUtility.m 9.8 KB

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