You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

506 lines
20 KiB

5 years ago
  1. //
  2. // FLEXFileBrowserTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/9/14.
  6. //
  7. //
  8. #import "FLEXFileBrowserTableViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXWebViewController.h"
  11. #import "FLEXImagePreviewViewController.h"
  12. #import "FLEXTableListViewController.h"
  13. #import "FLEXObjectExplorerFactory.h"
  14. #import "FLEXObjectExplorerViewController.h"
  15. @interface FLEXFileBrowserTableViewCell : UITableViewCell
  16. @end
  17. @interface FLEXFileBrowserTableViewController () <FLEXFileBrowserSearchOperationDelegate>
  18. @property (nonatomic, copy) NSString *path;
  19. @property (nonatomic, copy) NSArray<NSString *> *childPaths;
  20. @property (nonatomic) NSArray<NSString *> *searchPaths;
  21. @property (nonatomic) NSNumber *recursiveSize;
  22. @property (nonatomic) NSNumber *searchPathsSize;
  23. @property (nonatomic) NSOperationQueue *operationQueue;
  24. @property (nonatomic) UIDocumentInteractionController *documentController;
  25. @end
  26. @implementation FLEXFileBrowserTableViewController
  27. + (instancetype)path:(NSString *)path
  28. {
  29. return [[self alloc] initWithPath:path];
  30. }
  31. - (id)init
  32. {
  33. return [self initWithPath:NSHomeDirectory()];
  34. }
  35. - (id)initWithPath:(NSString *)path
  36. {
  37. self = [super init];
  38. if (self) {
  39. self.path = path;
  40. self.title = [path lastPathComponent];
  41. self.operationQueue = [NSOperationQueue new];
  42. //computing path size
  43. FLEXFileBrowserTableViewController *__weak weakSelf = self;
  44. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  45. NSFileManager *fileManager = NSFileManager.defaultManager;
  46. NSDictionary<NSString *, id> *attributes = [fileManager attributesOfItemAtPath:path error:NULL];
  47. uint64_t totalSize = [attributes fileSize];
  48. for (NSString *fileName in [fileManager enumeratorAtPath:path]) {
  49. attributes = [fileManager attributesOfItemAtPath:[path stringByAppendingPathComponent:fileName] error:NULL];
  50. totalSize += [attributes fileSize];
  51. // Bail if the interested view controller has gone away.
  52. if (!weakSelf) {
  53. return;
  54. }
  55. }
  56. dispatch_async(dispatch_get_main_queue(), ^{
  57. FLEXFileBrowserTableViewController *__strong strongSelf = weakSelf;
  58. strongSelf.recursiveSize = @(totalSize);
  59. [strongSelf.tableView reloadData];
  60. });
  61. });
  62. [self reloadCurrentPath];
  63. }
  64. return self;
  65. }
  66. #pragma mark - UIViewController
  67. - (void)viewDidLoad
  68. {
  69. [super viewDidLoad];
  70. self.showsSearchBar = YES;
  71. self.searchBarDebounceInterval = kFLEXDebounceForAsyncSearch;
  72. }
  73. #pragma mark - FLEXGlobalsEntry
  74. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
  75. switch (row) {
  76. case FLEXGlobalsRowBrowseBundle: return @"📁 Browse Bundle Directory";
  77. case FLEXGlobalsRowBrowseContainer: return @"📁 Browse Container Directory";
  78. default: return nil;
  79. }
  80. }
  81. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
  82. switch (row) {
  83. case FLEXGlobalsRowBrowseBundle: return [[self alloc] initWithPath:NSBundle.mainBundle.bundlePath];
  84. case FLEXGlobalsRowBrowseContainer: return [[self alloc] initWithPath:NSHomeDirectory()];
  85. default: return [self new];
  86. }
  87. }
  88. #pragma mark - FLEXFileBrowserSearchOperationDelegate
  89. - (void)fileBrowserSearchOperationResult:(NSArray<NSString *> *)searchResult size:(uint64_t)size
  90. {
  91. self.searchPaths = searchResult;
  92. self.searchPathsSize = @(size);
  93. [self.tableView reloadData];
  94. }
  95. #pragma mark - Search bar
  96. - (void)updateSearchResults:(NSString *)newText
  97. {
  98. [self reloadDisplayedPaths];
  99. }
  100. #pragma mark UISearchControllerDelegate
  101. - (void)willDismissSearchController:(UISearchController *)searchController
  102. {
  103. [self.operationQueue cancelAllOperations];
  104. [self reloadCurrentPath];
  105. [self.tableView reloadData];
  106. }
  107. #pragma mark - Table view data source
  108. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  109. {
  110. return 1;
  111. }
  112. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  113. {
  114. return self.searchController.isActive ? self.searchPaths.count : self.childPaths.count;
  115. }
  116. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  117. {
  118. BOOL isSearchActive = self.searchController.isActive;
  119. NSNumber *currentSize = isSearchActive ? self.searchPathsSize : self.recursiveSize;
  120. NSArray<NSString *> *currentPaths = isSearchActive ? self.searchPaths : self.childPaths;
  121. NSString *sizeString = nil;
  122. if (!currentSize) {
  123. sizeString = @"Computing size…";
  124. } else {
  125. sizeString = [NSByteCountFormatter stringFromByteCount:[currentSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile];
  126. }
  127. return [NSString stringWithFormat:@"%lu files (%@)", (unsigned long)currentPaths.count, sizeString];
  128. }
  129. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  130. {
  131. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  132. NSDictionary<NSString *, id> *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:fullPath error:NULL];
  133. BOOL isDirectory = [attributes.fileType isEqual:NSFileTypeDirectory];
  134. NSString *subtitle = nil;
  135. if (isDirectory) {
  136. NSUInteger count = [NSFileManager.defaultManager contentsOfDirectoryAtPath:fullPath error:NULL].count;
  137. subtitle = [NSString stringWithFormat:@"%lu item%@", (unsigned long)count, (count == 1 ? @"" : @"s")];
  138. } else {
  139. NSString *sizeString = [NSByteCountFormatter stringFromByteCount:attributes.fileSize countStyle:NSByteCountFormatterCountStyleFile];
  140. subtitle = [NSString stringWithFormat:@"%@ - %@", sizeString, attributes.fileModificationDate ?: @"Never modified"];
  141. }
  142. static NSString *textCellIdentifier = @"textCell";
  143. static NSString *imageCellIdentifier = @"imageCell";
  144. UITableViewCell *cell = nil;
  145. // Separate image and text only cells because otherwise the separator lines get out-of-whack on image cells reused with text only.
  146. UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
  147. NSString *cellIdentifier = image ? imageCellIdentifier : textCellIdentifier;
  148. if (!cell) {
  149. cell = [[FLEXFileBrowserTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
  150. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  151. cell.detailTextLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  152. cell.detailTextLabel.textColor = UIColor.grayColor;
  153. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  154. }
  155. NSString *cellTitle = [fullPath lastPathComponent];
  156. cell.textLabel.text = cellTitle;
  157. cell.detailTextLabel.text = subtitle;
  158. if (image) {
  159. cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
  160. cell.imageView.image = image;
  161. }
  162. return cell;
  163. }
  164. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  165. {
  166. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  167. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  168. NSString *subpath = fullPath.lastPathComponent;
  169. NSString *pathExtension = subpath.pathExtension;
  170. BOOL isDirectory = NO;
  171. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:fullPath isDirectory:&isDirectory];
  172. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  173. UIImage *image = cell.imageView.image;
  174. if (!stillExists) {
  175. [FLEXAlert showAlert:@"File Not Found" message:@"The file at the specified path no longer exists." from:self];
  176. [self reloadDisplayedPaths];
  177. return;
  178. }
  179. UIViewController *drillInViewController = nil;
  180. if (isDirectory) {
  181. drillInViewController = [[[self class] alloc] initWithPath:fullPath];
  182. } else if (image) {
  183. drillInViewController = [[FLEXImagePreviewViewController alloc] initWithImage:image];
  184. } else {
  185. NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
  186. if (!fileData.length) {
  187. [FLEXAlert showAlert:@"Empty File" message:@"No data returned from the file." from:self];
  188. return;
  189. }
  190. // Special case keyed archives, json, and plists to get more readable data.
  191. NSString *prettyString = nil;
  192. if ([pathExtension isEqualToString:@"json"]) {
  193. prettyString = [FLEXUtility prettyJSONStringFromData:fileData];
  194. } else {
  195. // Regardless of file extension...
  196. id object = nil;
  197. @try {
  198. // Try to decode an archived object regardless of file extension
  199. object = [NSKeyedUnarchiver unarchiveObjectWithData:fileData];
  200. } @catch (NSException *e) { }
  201. // Try to decode other things instead
  202. object = object
  203. ?: [NSPropertyListSerialization propertyListWithData:fileData
  204. options:0
  205. format:NULL
  206. error:NULL]
  207. ?: [NSDictionary dictionaryWithContentsOfFile:fullPath]
  208. ?: [NSArray arrayWithContentsOfFile:fullPath];
  209. if (object) {
  210. drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:object];
  211. }
  212. }
  213. if (prettyString.length) {
  214. drillInViewController = [[FLEXWebViewController alloc] initWithText:prettyString];
  215. } else if ([FLEXWebViewController supportsPathExtension:pathExtension]) {
  216. drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
  217. } else if ([FLEXTableListViewController supportsExtension:pathExtension]) {
  218. drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
  219. }
  220. else if (!drillInViewController) {
  221. NSString *fileString = [NSString stringWithUTF8String:fileData.bytes];
  222. if (fileString.length) {
  223. drillInViewController = [[FLEXWebViewController alloc] initWithText:fileString];
  224. }
  225. }
  226. }
  227. if (drillInViewController) {
  228. drillInViewController.title = subpath.lastPathComponent;
  229. [self.navigationController pushViewController:drillInViewController animated:YES];
  230. } else {
  231. // Share the file otherwise
  232. [self openFileController:fullPath];
  233. }
  234. }
  235. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  236. {
  237. UIMenuItem *rename = [[UIMenuItem alloc] initWithTitle:@"Rename" action:@selector(fileBrowserRename:)];
  238. UIMenuItem *delete = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(fileBrowserDelete:)];
  239. UIMenuItem *copyPath = [[UIMenuItem alloc] initWithTitle:@"Copy Path" action:@selector(fileBrowserCopyPath:)];
  240. UIMenuItem *share = [[UIMenuItem alloc] initWithTitle:@"Share" action:@selector(fileBrowserShare:)];
  241. UIMenuController.sharedMenuController.menuItems = @[rename, delete, copyPath, share];
  242. return YES;
  243. }
  244. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  245. {
  246. return action == @selector(fileBrowserDelete:)
  247. || action == @selector(fileBrowserRename:)
  248. || action == @selector(fileBrowserCopyPath:)
  249. || action == @selector(fileBrowserShare:);
  250. }
  251. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  252. {
  253. // Empty, but has to exist for the menu to show
  254. // The table view only calls this method for actions in the UIResponderStandardEditActions informal protocol.
  255. // Since our actions are outside of that protocol, we need to manually handle the action forwarding from the cells.
  256. }
  257. #if FLEX_AT_LEAST_IOS13_SDK
  258. - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0)
  259. {
  260. __weak typeof(self) weakSelf = self;
  261. return [UIContextMenuConfiguration configurationWithIdentifier:nil
  262. previewProvider:nil
  263. actionProvider:^UIMenu * _Nullable(NSArray<UIMenuElement *> * _Nonnull suggestedActions) {
  264. UITableViewCell * const cell = [tableView cellForRowAtIndexPath:indexPath];
  265. UIAction *rename = [UIAction actionWithTitle:@"Rename"
  266. image:nil
  267. identifier:@"Rename"
  268. handler:^(__kindof UIAction * _Nonnull action) {
  269. [weakSelf fileBrowserRename:cell];
  270. }];
  271. UIAction *delete = [UIAction actionWithTitle:@"Delete"
  272. image:nil
  273. identifier:@"Delete"
  274. handler:^(__kindof UIAction * _Nonnull action) {
  275. [weakSelf fileBrowserDelete:cell];
  276. }];
  277. UIAction *copyPath = [UIAction actionWithTitle:@"Copy Path"
  278. image:nil
  279. identifier:@"Copy Path"
  280. handler:^(__kindof UIAction * _Nonnull action) {
  281. [weakSelf fileBrowserCopyPath:cell];
  282. }];
  283. UIAction *share = [UIAction actionWithTitle:@"Share"
  284. image:nil
  285. identifier:@"Share"
  286. handler:^(__kindof UIAction * _Nonnull action) {
  287. [weakSelf fileBrowserShare:cell];
  288. }];
  289. return [UIMenu menuWithTitle:@"Manage File" image:nil identifier:@"Manage File" options:UIMenuOptionsDisplayInline children:@[rename, delete, copyPath, share]];
  290. }];
  291. }
  292. #endif
  293. - (void)openFileController:(NSString *)fullPath
  294. {
  295. UIDocumentInteractionController *controller = [UIDocumentInteractionController new];
  296. controller.URL = [NSURL fileURLWithPath:fullPath];
  297. [controller presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
  298. self.documentController = controller;
  299. }
  300. - (void)fileBrowserRename:(UITableViewCell *)sender
  301. {
  302. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  303. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  304. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:self.path isDirectory:NULL];
  305. if (stillExists) {
  306. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  307. make.title([NSString stringWithFormat:@"Rename %@?", fullPath.lastPathComponent]);
  308. make.configuredTextField(^(UITextField *textField) {
  309. textField.placeholder = @"New file name";
  310. textField.text = fullPath.lastPathComponent;
  311. });
  312. make.button(@"Rename").handler(^(NSArray<NSString *> *strings) {
  313. NSString *newFileName = strings.firstObject;
  314. NSString *newPath = [fullPath.stringByDeletingLastPathComponent stringByAppendingPathComponent:newFileName];
  315. [NSFileManager.defaultManager moveItemAtPath:fullPath toPath:newPath error:NULL];
  316. [self reloadDisplayedPaths];
  317. });
  318. make.button(@"Cancel").cancelStyle();
  319. } showFrom:self];
  320. } else {
  321. [FLEXAlert showAlert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
  322. }
  323. }
  324. - (void)fileBrowserDelete:(UITableViewCell *)sender
  325. {
  326. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  327. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  328. BOOL isDirectory = NO;
  329. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:fullPath isDirectory:&isDirectory];
  330. if (stillExists) {
  331. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  332. make.title(@"Confirm Deletion");
  333. make.message([NSString stringWithFormat:
  334. @"The %@ '%@' will be deleted. This operation cannot be undone",
  335. (isDirectory ? @"directory" : @"file"), fullPath.lastPathComponent
  336. ]);
  337. make.button(@"Delete").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  338. [NSFileManager.defaultManager removeItemAtPath:fullPath error:NULL];
  339. [self reloadDisplayedPaths];
  340. });
  341. make.button(@"Cancel").cancelStyle();
  342. } showFrom:self];
  343. } else {
  344. [FLEXAlert showAlert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
  345. }
  346. }
  347. - (void)fileBrowserCopyPath:(UITableViewCell *)sender
  348. {
  349. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  350. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  351. UIPasteboard.generalPasteboard.string = fullPath;
  352. }
  353. - (void)fileBrowserShare:(UITableViewCell *)sender
  354. {
  355. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  356. NSString *pathString = [self filePathAtIndexPath:indexPath];
  357. NSURL *filePath = [NSURL fileURLWithPath:pathString];
  358. BOOL isDirectory = NO;
  359. [NSFileManager.defaultManager fileExistsAtPath:pathString isDirectory:&isDirectory];
  360. if (isDirectory) {
  361. // UIDocumentInteractionController for folders
  362. [self openFileController:pathString];
  363. } else {
  364. // Share sheet for files
  365. UIActivityViewController *shareSheet = [[UIActivityViewController alloc] initWithActivityItems:@[filePath] applicationActivities:nil];
  366. [self presentViewController:shareSheet animated:true completion:nil];
  367. }
  368. }
  369. - (void)reloadDisplayedPaths
  370. {
  371. if (self.searchController.isActive) {
  372. [self updateSearchPaths];
  373. } else {
  374. [self reloadCurrentPath];
  375. [self.tableView reloadData];
  376. }
  377. }
  378. - (void)reloadCurrentPath
  379. {
  380. NSMutableArray<NSString *> *childPaths = [NSMutableArray array];
  381. NSArray<NSString *> *subpaths = [NSFileManager.defaultManager contentsOfDirectoryAtPath:self.path error:NULL];
  382. for (NSString *subpath in subpaths) {
  383. [childPaths addObject:[self.path stringByAppendingPathComponent:subpath]];
  384. }
  385. self.childPaths = childPaths;
  386. }
  387. - (void)updateSearchPaths
  388. {
  389. self.searchPaths = nil;
  390. self.searchPathsSize = nil;
  391. //clear pre search request and start a new one
  392. [self.operationQueue cancelAllOperations];
  393. FLEXFileBrowserSearchOperation *newOperation = [[FLEXFileBrowserSearchOperation alloc] initWithPath:self.path searchString:self.searchText];
  394. newOperation.delegate = self;
  395. [self.operationQueue addOperation:newOperation];
  396. }
  397. - (NSString *)filePathAtIndexPath:(NSIndexPath *)indexPath
  398. {
  399. return self.searchController.isActive ? self.searchPaths[indexPath.row] : self.childPaths[indexPath.row];
  400. }
  401. @end
  402. @implementation FLEXFileBrowserTableViewCell
  403. - (void)forwardAction:(SEL)action withSender:(id)sender
  404. {
  405. id target = [self.nextResponder targetForAction:action withSender:sender];
  406. [UIApplication.sharedApplication sendAction:action to:target from:self forEvent:nil];
  407. }
  408. - (void)fileBrowserRename:(UIMenuController *)sender
  409. {
  410. [self forwardAction:_cmd withSender:sender];
  411. }
  412. - (void)fileBrowserDelete:(UIMenuController *)sender
  413. {
  414. [self forwardAction:_cmd withSender:sender];
  415. }
  416. - (void)fileBrowserCopyPath:(UIMenuController *)sender
  417. {
  418. [self forwardAction:_cmd withSender:sender];
  419. }
  420. - (void)fileBrowserShare:(UIMenuController *)sender
  421. {
  422. [self forwardAction:_cmd withSender:sender];
  423. }
  424. @end