QNFileRecorder.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // QNFileRecorder.m
  3. // QiniuSDK
  4. //
  5. // Created by bailong on 14/10/5.
  6. // Copyright (c) 2014年 Qiniu. All rights reserved.
  7. //
  8. #import "QNFileRecorder.h"
  9. #import "QNUrlSafeBase64.h"
  10. @interface QNFileRecorder ()
  11. @property (copy, readonly) NSString *directory;
  12. @property BOOL encode;
  13. @end
  14. @implementation QNFileRecorder
  15. - (NSString *)pathOfKey:(NSString *)key {
  16. return [QNFileRecorder pathJoin:key path:_directory];
  17. }
  18. + (NSString *)pathJoin:(NSString *)key
  19. path:(NSString *)path {
  20. return [[NSString alloc] initWithFormat:@"%@/%@", path, key];
  21. }
  22. + (instancetype)fileRecorderWithFolder:(NSString *)directory
  23. error:(NSError *__autoreleasing *)perror {
  24. return [QNFileRecorder fileRecorderWithFolder:directory encodeKey:false error:perror];
  25. }
  26. + (instancetype)fileRecorderWithFolder:(NSString *)directory
  27. encodeKey:(BOOL)encode
  28. error:(NSError *__autoreleasing *)perror {
  29. NSError *error;
  30. [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:&error];
  31. if (error != nil) {
  32. if (perror) {
  33. *perror = error;
  34. }
  35. return nil;
  36. }
  37. return [[QNFileRecorder alloc] initWithFolder:directory encodeKey:encode];
  38. }
  39. - (instancetype)initWithFolder:(NSString *)directory encodeKey:(BOOL)encode {
  40. if (self = [super init]) {
  41. _directory = directory;
  42. _encode = encode;
  43. }
  44. return self;
  45. }
  46. - (NSError *)set:(NSString *)key
  47. data:(NSData *)value {
  48. NSError *error;
  49. if (_encode) {
  50. key = [QNUrlSafeBase64 encodeString:key];
  51. }
  52. [value writeToFile:[self pathOfKey:key] options:NSDataWritingAtomic error:&error];
  53. return error;
  54. }
  55. - (NSData *)get:(NSString *)key {
  56. if (_encode) {
  57. key = [QNUrlSafeBase64 encodeString:key];
  58. }
  59. return [NSData dataWithContentsOfFile:[self pathOfKey:key]];
  60. }
  61. - (NSError *)del:(NSString *)key {
  62. NSError *error;
  63. if (_encode) {
  64. key = [QNUrlSafeBase64 encodeString:key];
  65. }
  66. [[NSFileManager defaultManager] removeItemAtPath:[self pathOfKey:key] error:&error];
  67. return error;
  68. }
  69. - (NSString *)getFileName{
  70. return nil;
  71. }
  72. + (void)removeKey:(NSString *)key
  73. directory:(NSString *)dir
  74. encodeKey:(BOOL)encode {
  75. if (encode) {
  76. key = [QNUrlSafeBase64 encodeString:key];
  77. }
  78. NSError *error;
  79. NSString *path = [QNFileRecorder pathJoin:key path:dir];
  80. [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
  81. if (error) {
  82. NSLog(@"%s,%@", __func__, error);
  83. }
  84. }
  85. - (NSString *)description {
  86. return [NSString stringWithFormat:@"<%@: %p, dir: %@>", NSStringFromClass([self class]), self, _directory];
  87. }
  88. @end