1
2#import <ExpoFileSystem/EXFileSystemLocalFileHandler.h>
3#import <ExpoFileSystem/NSData+EXFileSystem.h>
4
5@implementation EXFileSystemLocalFileHandler
6
7+ (void)getInfoForFile:(NSURL *)fileUri
8           withOptions:(NSDictionary *)options
9              resolver:(EXPromiseResolveBlock)resolve
10              rejecter:(EXPromiseRejectBlock)reject
11{
12  NSString *path = fileUri.path;
13  BOOL isDirectory;
14  if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) {
15    NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
16    NSMutableDictionary *result = [NSMutableDictionary dictionary];
17    result[@"exists"] = @(YES);
18    result[@"isDirectory"] = @(isDirectory);
19    result[@"uri"] = [NSURL fileURLWithPath:path].absoluteString;
20    if (options[@"md5"]) {
21      result[@"md5"] = [[NSData dataWithContentsOfFile:path] md5String];
22    }
23    result[@"size"] = @([EXFileSystemLocalFileHandler getFileSize:path attributes:attributes]);
24    result[@"modificationTime"] = @(attributes.fileModificationDate.timeIntervalSince1970);
25    resolve(result);
26  } else {
27    resolve(@{@"exists": @(NO), @"isDirectory": @(NO)});
28  }
29}
30
31+ (unsigned long long)getFileSize:(NSString *)path attributes:(NSDictionary<NSFileAttributeKey, id> *)attributes
32{
33  if (attributes.fileType != NSFileTypeDirectory) {
34    return attributes.fileSize;
35  }
36
37  // The path is pointing to the folder
38  NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
39  NSEnumerator *contentsEnumurator = [contents objectEnumerator];
40  NSString *file;
41  unsigned long long folderSize = 0;
42  while (file = [contentsEnumurator nextObject]) {
43    NSString *filePath = [path stringByAppendingPathComponent:file];
44    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
45    folderSize += [EXFileSystemLocalFileHandler getFileSize:filePath attributes:fileAttributes];
46  }
47
48  return folderSize;
49}
50
51+ (void)copyFrom:(NSURL *)from
52              to:(NSURL *)to
53        resolver:(EXPromiseResolveBlock)resolve
54        rejecter:(EXPromiseRejectBlock)reject
55{
56  NSString *fromPath = [from.path stringByStandardizingPath];
57  NSString *toPath = [to.path stringByStandardizingPath];
58
59  NSError *error;
60  if ([[NSFileManager defaultManager] fileExistsAtPath:toPath]) {
61    if (![[NSFileManager defaultManager] removeItemAtPath:toPath error:&error]) {
62      reject(@"E_FILE_NOT_COPIED",
63             [NSString stringWithFormat:@"File '%@' could not be copied to '%@' because a file already exists at "
64              "the destination and could not be deleted.", from, to],
65             error);
66      return;
67    }
68  }
69
70  if ([[NSFileManager defaultManager] copyItemAtPath:fromPath toPath:toPath error:&error]) {
71    resolve(nil);
72  } else {
73    reject(@"E_FILE_NOT_COPIED",
74           [NSString stringWithFormat:@"File '%@' could not be copied to '%@'.", from, to],
75           error);
76  }
77}
78
79@end
80