YHNativeFS.m
2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// YHNativeFS.m
// YohoExplorerDemo
//
// Created by gaoqiang xu on 4/13/15.
// Copyright (c) 2015 gaoqiang xu. All rights reserved.
//
#import "YHNativeFS.h"
NSString * const YHNative_FS = @"Native_FS";
@implementation YHNativeFS
- (NSArray *)availableFunctions
{
return @[ @"getDocumentsPath", @"getTemporaryPath", @"getCachePath", @"deleteItemAtPath", @"copyItem" ];
}
- (NSString *)actionName
{
return YHNative_FS;
}
- (void)getDocumentsPath
{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
self.successCallBack( @{ @"path":path }, NO, nil);
}
- (void)getTemporaryPath
{
NSString *temp = NSTemporaryDirectory();
self.successCallBack( @{ @"path":temp }, NO, nil );
}
- (void)getCachePath
{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
self.successCallBack( @{ @"path":path }, NO, nil);
}
- (void)deleteItemAtPath
{
if (self.options && self.options[@"path"]) {
NSString *path = self.options[@"path"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:nil]) {
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
if (error) {
self.failureCallBack(error.description);
} else {
self.successCallBack(@{@"path":path}, NO, nil);
}
} else {
self.failureCallBack([NSString stringWithFormat:@"File does not exist at path:%@ .", path]);
}
} else {
self.failureCallBack(@"Parameter invalid.");
}
}
- (void)copyItem
{
if (!self.options) {
self.failureCallBack(@"Parameter invalid.");
return;
}
NSString *src = self.options[@"srcPath"];
NSString *dst = self.options[@"dstPath"];
if (src.length == 0 || dst.length == 0) {
self.failureCallBack(@"Parameter invalid.");
return;
}
NSError *error;
BOOL ok = [[NSFileManager defaultManager] copyItemAtPath:src toPath:dst error:&error];
if (!ok) {
self.failureCallBack(error.description);
} else {
self.successCallBack(@{@"path":dst}, NO, nil);
}
}
@end