YHNativeFS.m 2.21 KB
//
//  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);
}

- (void)getTemporaryPath
{
    NSString *temp = NSTemporaryDirectory();
    self.successCallBack( @{ @"path":temp }, NO );
}

- (void)getCachePath
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    self.successCallBack( @{ @"path":path }, NO);
}

- (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);
            }
        } 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);
    }
}

@end