123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- //
- // QNFileRecorder.m
- // QiniuSDK
- //
- // Created by bailong on 14/10/5.
- // Copyright (c) 2014年 Qiniu. All rights reserved.
- //
- #import "QNFileRecorder.h"
- #import "QNUrlSafeBase64.h"
- @interface QNFileRecorder ()
- @property (copy, readonly) NSString *directory;
- @property BOOL encode;
- @end
- @implementation QNFileRecorder
- - (NSString *)pathOfKey:(NSString *)key {
- return [QNFileRecorder pathJoin:key path:_directory];
- }
- + (NSString *)pathJoin:(NSString *)key
- path:(NSString *)path {
- return [[NSString alloc] initWithFormat:@"%@/%@", path, key];
- }
- + (instancetype)fileRecorderWithFolder:(NSString *)directory
- error:(NSError *__autoreleasing *)perror {
- return [QNFileRecorder fileRecorderWithFolder:directory encodeKey:false error:perror];
- }
- + (instancetype)fileRecorderWithFolder:(NSString *)directory
- encodeKey:(BOOL)encode
- error:(NSError *__autoreleasing *)perror {
- NSError *error;
- [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:&error];
- if (error != nil) {
- if (perror) {
- *perror = error;
- }
- return nil;
- }
- return [[QNFileRecorder alloc] initWithFolder:directory encodeKey:encode];
- }
- - (instancetype)initWithFolder:(NSString *)directory encodeKey:(BOOL)encode {
- if (self = [super init]) {
- _directory = directory;
- _encode = encode;
- }
- return self;
- }
- - (NSError *)set:(NSString *)key
- data:(NSData *)value {
- NSError *error;
- if (_encode) {
- key = [QNUrlSafeBase64 encodeString:key];
- }
- [value writeToFile:[self pathOfKey:key] options:NSDataWritingAtomic error:&error];
- return error;
- }
- - (NSData *)get:(NSString *)key {
- if (_encode) {
- key = [QNUrlSafeBase64 encodeString:key];
- }
- return [NSData dataWithContentsOfFile:[self pathOfKey:key]];
- }
- - (NSError *)del:(NSString *)key {
- NSError *error;
- if (_encode) {
- key = [QNUrlSafeBase64 encodeString:key];
- }
- [[NSFileManager defaultManager] removeItemAtPath:[self pathOfKey:key] error:&error];
- return error;
- }
- + (void)removeKey:(NSString *)key
- directory:(NSString *)dir
- encodeKey:(BOOL)encode {
- if (encode) {
- key = [QNUrlSafeBase64 encodeString:key];
- }
- NSError *error;
- NSString *path = [QNFileRecorder pathJoin:key path:dir];
- [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
- if (error) {
- NSLog(@"%s,%@", __func__, error);
- }
- }
- - (NSString *)description {
- return [NSString stringWithFormat:@"<%@: %p, dir: %@>", NSStringFromClass([self class]), self, _directory];
- }
- @end
|