YHNativeFS.m 2.24 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, 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