123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- //
- // CCUtility.m
- // Nextcloud
- //
- // Created by Marino Faggiana on 02/02/16.
- // Copyright (c) 2016 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- #import "CCUtility.h"
- #import "NCBridgeSwift.h"
- #import "NSNotificationCenter+MainThread.h"
- #import <OpenSSL/OpenSSL.h>
- #import <CoreLocation/CoreLocation.h>
- #include <sys/stat.h>
- @implementation CCUtility
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Various =====
- #pragma --------------------------------------------------------------------------------------------
- + (NSString *)createFileName:(NSString *)fileName fileDate:(NSDate *)fileDate fileType:(PHAssetMediaType)fileType keyFileName:(NSString *)keyFileName keyFileNameType:(NSString *)keyFileNameType keyFileNameOriginal:(NSString *)keyFileNameOriginal forcedNewFileName:(BOOL)forcedNewFileName
- {
- BOOL addFileNameType = NO;
- // Original FileName ?
- if ([[[NCKeychain alloc] init] getOriginalFileNameWithKey:keyFileNameOriginal] && !forcedNewFileName) {
- return fileName;
- }
- NSString *numberFileName;
- if ([fileName length] > 8) numberFileName = [fileName substringWithRange:NSMakeRange(04, 04)];
- else numberFileName = [[NCKeychain alloc] init].incrementalNumber;
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
- [formatter setDateFormat:@"yy-MM-dd HH-mm-ss"];
- NSString *fileNameDate = [formatter stringFromDate:fileDate];
- NSString *fileNameType = @"";
- if (fileType == PHAssetMediaTypeImage)
- fileNameType = NSLocalizedString(@"_photo_", nil);
- if (fileType == PHAssetMediaTypeVideo)
- fileNameType = NSLocalizedString(@"_video_", nil);
- if (fileType == PHAssetMediaTypeAudio)
- fileNameType = NSLocalizedString(@"_audio_", nil);
- if (fileType == PHAssetMediaTypeUnknown)
- fileNameType = NSLocalizedString(@"_unknown_", nil);
- // Use File Name Type
- if (keyFileNameType)
- addFileNameType = [[[NCKeychain alloc] init] getFileNameTypeWithKey:keyFileNameType];
- NSString *fileNameExt = [[fileName pathExtension] lowercaseString];
- if (keyFileName) {
- fileName = [[[NCKeychain alloc] init] getFileNameMaskWithKey:keyFileName];
- if ([fileName length] > 0) {
- [formatter setDateFormat:@"dd"];
- NSString *dayNumber = [formatter stringFromDate:fileDate];
- [formatter setDateFormat:@"MMM"];
- NSString *month = [formatter stringFromDate:fileDate];
- [formatter setDateFormat:@"MM"];
- NSString *monthNumber = [formatter stringFromDate:fileDate];
- [formatter setDateFormat:@"yyyy"];
- NSString *year = [formatter stringFromDate:fileDate];
- [formatter setDateFormat:@"yy"];
- NSString *yearNumber = [formatter stringFromDate:fileDate];
- [formatter setDateFormat:@"HH"];
- NSString *hour24 = [formatter stringFromDate:fileDate];
- [formatter setDateFormat:@"hh"];
- NSString *hour12 = [formatter stringFromDate:fileDate];
- [formatter setDateFormat:@"mm"];
- NSString *minute = [formatter stringFromDate:fileDate];
- [formatter setDateFormat:@"ss"];
- NSString *second = [formatter stringFromDate:fileDate];
- [formatter setDateFormat:@"a"];
- NSString *ampm = [formatter stringFromDate:fileDate];
- // Replace string with date
- fileName = [fileName stringByReplacingOccurrencesOfString:@"DD" withString:dayNumber];
- fileName = [fileName stringByReplacingOccurrencesOfString:@"MMM" withString:month];
- fileName = [fileName stringByReplacingOccurrencesOfString:@"MM" withString:monthNumber];
- fileName = [fileName stringByReplacingOccurrencesOfString:@"YYYY" withString:year];
- fileName = [fileName stringByReplacingOccurrencesOfString:@"YY" withString:yearNumber];
- fileName = [fileName stringByReplacingOccurrencesOfString:@"HH" withString:hour24];
- fileName = [fileName stringByReplacingOccurrencesOfString:@"hh" withString:hour12];
- fileName = [fileName stringByReplacingOccurrencesOfString:@"mm" withString:minute];
- fileName = [fileName stringByReplacingOccurrencesOfString:@"ss" withString:second];
- fileName = [fileName stringByReplacingOccurrencesOfString:@"ampm" withString:ampm];
- if (addFileNameType)
- fileName = [NSString stringWithFormat:@"%@%@%@.%@", fileNameType, fileName, numberFileName, fileNameExt];
- else
- fileName = [NSString stringWithFormat:@"%@%@.%@", fileName, numberFileName, fileNameExt];
- fileName = [fileName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- } else {
- if (addFileNameType)
- fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
- else
- fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
- }
- } else {
- if (addFileNameType)
- fileName = [NSString stringWithFormat:@"%@ %@ %@.%@", fileNameType, fileNameDate, numberFileName, fileNameExt];
- else
- fileName = [NSString stringWithFormat:@"%@ %@.%@", fileNameDate, numberFileName, fileNameExt];
- }
- return fileName;
- }
- + (NSString *)getMimeType:(NSString *)fileNameView
- {
- CFStringRef fileUTI = nil;
- NSString *returnFileUTI = nil;
- if ([fileNameView isEqualToString:@"."]) {
- return returnFileUTI;
- } else {
- CFStringRef fileExtension = (__bridge CFStringRef)[fileNameView pathExtension];
- NSString *ext = (__bridge NSString *)fileExtension;
- ext = ext.uppercaseString;
- fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
- if (fileUTI != nil) {
- returnFileUTI = (__bridge NSString *)fileUTI;
- CFRelease(fileUTI);
- }
- }
- return returnFileUTI;
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Share Permissions =====
- #pragma --------------------------------------------------------------------------------------------
- + (NSInteger) getPermissionsValueByCanEdit:(BOOL)canEdit andCanCreate:(BOOL)canCreate andCanChange:(BOOL)canChange andCanDelete:(BOOL)canDelete andCanShare:(BOOL)canShare andIsFolder:(BOOL) isFolder
- {
- NSInteger permissionsValue = NCGlobal.shared.permissionReadShare;
- if (canEdit && !isFolder) {
- permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
- }
- if (canCreate & isFolder) {
- permissionsValue = permissionsValue + NCGlobal.shared.permissionCreateShare;
- }
- if (canChange && isFolder) {
- permissionsValue = permissionsValue + NCGlobal.shared.permissionUpdateShare;
- }
- if (canDelete & isFolder) {
- permissionsValue = permissionsValue + NCGlobal.shared.permissionDeleteShare;
- }
- if (canShare) {
- permissionsValue = permissionsValue + NCGlobal.shared.permissionShareShare;
- }
- return permissionsValue;
- }
- + (BOOL) isPermissionToCanCreate:(NSInteger) permissionValue {
- BOOL canCreate = ((permissionValue & NCGlobal.shared.permissionCreateShare) > 0);
- return canCreate;
- }
- + (BOOL) isPermissionToCanChange:(NSInteger) permissionValue {
- BOOL canChange = ((permissionValue & NCGlobal.shared.permissionUpdateShare) > 0);
- return canChange;
- }
- + (BOOL) isPermissionToCanDelete:(NSInteger) permissionValue {
- BOOL canDelete = ((permissionValue & NCGlobal.shared.permissionDeleteShare) > 0);
- return canDelete;
- }
- + (BOOL) isPermissionToCanShare:(NSInteger) permissionValue {
- BOOL canShare = ((permissionValue & NCGlobal.shared.permissionShareShare) > 0);
- return canShare;
- }
- + (BOOL) isAnyPermissionToEdit:(NSInteger) permissionValue {
- BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
- BOOL canChange = [self isPermissionToCanChange:permissionValue];
- BOOL canDelete = [self isPermissionToCanDelete:permissionValue];
- BOOL canEdit = (canCreate || canChange || canDelete);
- return canEdit;
- }
- + (BOOL) isPermissionToRead:(NSInteger) permissionValue {
- BOOL canRead = ((permissionValue & NCGlobal.shared.permissionReadShare) > 0);
- return canRead;
- }
- + (BOOL) isPermissionToReadCreateUpdate:(NSInteger) permissionValue {
- BOOL canRead = [self isPermissionToRead:permissionValue];
- BOOL canCreate = [self isPermissionToCanCreate:permissionValue];
- BOOL canChange = [self isPermissionToCanChange:permissionValue];
- BOOL canEdit = (canCreate && canChange && canRead);
- return canEdit;
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Third parts =====
- #pragma --------------------------------------------------------------------------------------------
- + (NSDate *)getATime:(const char *)path
- {
- struct stat st;
- stat(path, &st);
- time_t accessed = st.st_atime;
- NSDate *date = [NSDate dateWithTimeIntervalSince1970:accessed];
- return date;
- }
- @end
|