메시지 기능 개발하다가 만든 업로드/다운로드 때 파일생성 관련 코드
#import <Foundation/Foundation.h>
#import <GlobalUtils.h>
#import "Constants.h"
typedef NS_ENUM(NSInteger, FileCacheType) {
FileCacheUpload,
FileCacheDownload
};
@interface FileCacheManager : NSObject
@property(nonatomic, assign) NSString* archiveDirectoryPath;
@property(nonatomic, assign) NSString* cacheDirectoryPath;
@property(nonatomic, assign) NSString* uploadTempDirectoryPath;
@property(nonatomic, assign) NSString* downloadDirectoryPath;
- (BOOL)createDirectoryIfNotExist:(NSString *)path;
- (NSString *)createTempFile:(FileCacheType)type data:(NSData *)data extension:(NSString *)extension;
- (NSString *)createTempFileName:(NSString *)extension;
- (NSString *)createTempFileWithName:(FileCacheType)type name:(NSString *)fileName data:(NSData *)data;
- (BOOL)saveFile:(NSString *)fileName;
- (BOOL)deleteFile:(NSString *)fileName;
- (NSData *)dataWithFileFromCache:(NSString *)fileName;
- (NSData *)dataWithFileFromArchive:(NSString *)fileName;
- (BOOL)isExistFile:(NSString *)path fileName:(NSString *)fileName;
- (BOOL)copyToCache:(NSString *)fileName;
- (BOOL)copyToArchive:(NSString *)fileName;
@end
#import "FileCacheManager.h"
@interface FileCacheManager()
@end
@implementation FileCacheManager
- (NSString *)archiveDirectoryPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [NSString stringWithFormat:@"%@/%@", documentsDirectory, ARCHIVE_FOLDER];
}
- (NSString *)cacheDirectoryPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
return cachesDirectory;
}
- (BOOL)createDirectoryIfNotExist:(NSString *)path
{
if (![[NSFileManager defaultManager] fileExistsAtPath:path]){
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&error];
if (error) {
KLog(@"[FileCacheManager] Failed to create temp folder");
return NO;
}
}
return YES;
}
- (NSString *)createTempFileWithName:(FileCacheType)type name:(NSString *)fileName data:(NSData *)data
{
NSString *cacheDirPath = type == FileCacheUpload ? self.cacheDirectoryPath : self.archiveDirectoryPath;
// cache 폴더 존재유무 확인 및 생성
if([self createDirectoryIfNotExist:cacheDirPath]) {
// cache 폴더에 파일이 존재하는지 화인
NSString *filePath = [NSString stringWithFormat:@"%@/%@", cacheDirPath, fileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
// 없다면 임시 파일을 생성
BOOL isCreated = [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];
if(!isCreated) {
return nil;
}
}
return fileName;
}
return fileName;
}
- (BOOL)copyToCache:(NSString *)fileName
{
if([self isExistInArchive:fileName]) {
NSString *filePath = [NSString stringWithFormat:@"%@/%@", self.archiveDirectoryPath, fileName];
NSData * data = [[NSFileManager defaultManager] contentsAtPath:filePath];
NSString *result = [self createTempFileWithName:FileCacheUpload name:fileName data:data];
if(result != nil) {
return YES;
}
}
return NO;
}
- (BOOL)copyToArchive:(NSString *)fileName
{
if([self isExistInCache:fileName]) {
NSString *filePath = [NSString stringWithFormat:@"%@/%@", self.cacheDirectoryPath, fileName];
NSData * data = [[NSFileManager defaultManager] contentsAtPath:filePath];
NSString *result = [self createTempFileWithName:FileCacheDownload name:fileName data:data];
if(result != nil) {
return YES;
}
}
return NO;
}
- (NSString *)createTempFile:(FileCacheType)type data:(NSData *)data extension:(NSString *)extension
{
NSString *fileName = [self createTempFileName:extension];
return [self createTempFileWithName:type name:fileName data:data];
}
- (NSString *)createTempFileName:(NSString *)extension
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyMMddHHmmssSSS"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"CC_%@.%@", dateString, extension];
return fileName;
}
- (BOOL)saveFile:(NSString *)fileName
{
return YES;
}
- (BOOL)deleteFile:(NSString *)fileName
{
return YES;
}
- (NSData *)dataWithFileFromCache:(NSString *)fileName
{
NSString *filePath = [NSString stringWithFormat:@"%@/%@", self.cacheDirectoryPath, fileName];
return [NSData dataWithContentsOfFile:filePath];
}
- (NSData *)dataWithFileFromArchive:(NSString *)fileName
{
return [[NSData alloc] init];
}
- (BOOL)isExistInCache:(NSString *)fileName
{
return [self isExistFile:self.cacheDirectoryPath fileName:fileName];
}
- (BOOL)isExistInArchive:(NSString *)fileName
{
return [self isExistFile:self.archiveDirectoryPath fileName:fileName];
}
- (BOOL)isExistFile:(NSString *)path fileName:(NSString *)fileName
{
NSString *filePath = [NSString stringWithFormat:@"%@/%@", path, fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
return YES;
}
return NO;
}
@end