Today's Menu  Portugal
journal and plan nutrition
WebMover.m
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014 Lee Barney
3  Permission is hereby granted, free of charge, to any person obtaining a
4  copy of this software and associated documentation files (the "Software"),
5  to deal in the Software without restriction, including without limitation the
6  rights to use, copy, modify, merge, publish, distribute, sublicense,
7  and/or sell copies of the Software, and to permit persons to whom the Software
8  is furnished to do so, subject to the following conditions:
9 
10  The above copyright notice and this permission notice shall be
11  included in all copies or substantial portions of the Software.
12 
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
18  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
19  OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 
21 
22  */
23 
24 #import "WebMover.h"
25 
26 @interface WebMover ()
27 +(NSString*)moveWebFiles:(NSArray*)movableFileTypes error:(NSError**)error;
28 +(NSString*)moveDirectoriesSearchingForIndexHTML:(BOOL)searchFlag error:(NSError**)error;
29 +(NSString*)searchForIndexHTMLFile:(NSMutableArray*)resources atIndex:(int)curIndex usingManager:(NSFileManager*)fileManager error:(NSError**)error;
30 @end
31 
32 @implementation WebMover
33 
34 +(NSString*)moveDirectoriesAndWebFilesOfType:(NSArray*)webFileTypes error:(NSError**)error{
35  NSString *indexHTMLPath = nil;
36  if (webFileTypes != nil) {
37  NSError *moveError = nil;
38  indexHTMLPath = [WebMover moveWebFiles:webFileTypes error:&moveError];
39  if (moveError != nil) {
40  *error = moveError;
41  return nil;
42  }
43  }
44  NSError *moveError = nil;
45  NSString *foundIndexPath = [WebMover moveDirectoriesSearchingForIndexHTML:(indexHTMLPath == nil) error:&moveError];
46  if (moveError != nil) {
47  *error = moveError;
48  return nil;
49  }
50  if (indexHTMLPath == nil) {
51  indexHTMLPath = foundIndexPath;
52  }
53  return indexHTMLPath;
54 }
55 
56 
57 +(NSString*)moveWebFiles:(NSArray*)movableFileTypes error:(NSError**)error{
58 
59  NSFileManager *fileManager = [NSFileManager defaultManager];
60  NSString *indexPath = nil;
61  NSString *resourcesPath = [NSBundle mainBundle].resourcePath;
62  NSString *tempPath = NSTemporaryDirectory();
63  NSError *anError = nil;
64  NSArray *resourcesList = [fileManager contentsOfDirectoryAtPath:resourcesPath error:&anError];
65  if (anError != nil) {
66  *error = anError;
67  return nil;
68  }
69  for (NSString *resourceName in resourcesList) {
70  BOOL isMovableType = NO;
71  for (NSString *fileType in movableFileTypes) {
72  if ([resourceName.lowercaseString hasSuffix:fileType]) {
73  isMovableType = YES;
74  break;
75  }
76  }
77  if (isMovableType) {
78  NSString *destinationPath = [tempPath stringByAppendingPathComponent:resourceName];
79  if ([fileManager fileExistsAtPath:destinationPath]) {
80  NSError *removeError = nil;
81  [fileManager removeItemAtPath:destinationPath error:&removeError];
82  if (removeError != nil) {
83  *error = removeError;
84  return nil;
85  }
86  }
87  NSString *resourcePath = [resourcesPath stringByAppendingPathComponent:resourceName];
88  NSError *copyError = nil;
89  [fileManager copyItemAtPath:resourcePath toPath:destinationPath error:&copyError];
90  if (copyError != nil) {
91  *error = copyError;
92  return nil;
93  }
94  if ([resourceName isEqualToString:@"index.html"]) {
95  indexPath = destinationPath;
96  }
97  }
98  }
99 
100  return indexPath;
101 }
102 +(NSString*)moveDirectoriesSearchingForIndexHTML:(BOOL)searchFlag error:(NSError**)error{
103 
104  NSFileManager *fileManager = [NSFileManager defaultManager];
105  NSString *indexPath = nil;
106  NSString *resourcesPath = [NSBundle mainBundle].resourcePath;
107  NSString *tempPath = NSTemporaryDirectory();
108  NSError *anError = nil;
109  NSArray *resourcesList = [fileManager contentsOfDirectoryAtPath:resourcesPath error:&anError];
110  if (anError != nil) {
111  *error = anError;
112  return nil;
113  }
114  for (NSString *resourceName in resourcesList) {
115  BOOL isDirectoryType = NO;
116  NSString *resourcePath = [resourcesPath stringByAppendingPathComponent:resourceName];
117  NSString *destinationPath = [tempPath stringByAppendingPathComponent:resourceName];
118  [fileManager fileExistsAtPath:resourcePath isDirectory:&isDirectoryType];
119  if (isDirectoryType && ![resourceName hasSuffix:@".lproj"]
120  && ![resourceName isEqualToString:@"Frameworks"]
121  && ![resourceName isEqualToString:@"META-INF"]
122  && ![resourceName isEqualToString:@"_CodeSignature"]) {
123 
124  if ([fileManager fileExistsAtPath:destinationPath]) {
125  NSError *removeError = nil;
126  [fileManager removeItemAtPath:destinationPath error:&removeError];
127  if (removeError != nil) {
128  *error = removeError;
129  return nil;
130  }
131  }
132  NSError *copyError = nil;
133  [fileManager copyItemAtPath:resourcePath toPath:destinationPath error:&copyError];
134  if (copyError != nil) {
135  *error = copyError;
136  return nil;
137  }
138  else if(searchFlag){
139  NSMutableArray *resources = [NSMutableArray array];;
140  NSError *contentsError = nil;
141  NSArray *childResourcesList = [fileManager contentsOfDirectoryAtPath:destinationPath error:&contentsError];
142  if (contentsError != nil) {
143  continue;
144  }
145  for (NSString *childResource in childResourcesList) {
146  [resources addObject:@[destinationPath,childResource]];
147  }
148  NSError *searchError = nil;
149  NSString *indexHTMLFilePath = [WebMover searchForIndexHTMLFile:resources atIndex:0 usingManager:fileManager error:&searchError];
150  if (indexHTMLFilePath != nil) {
151  indexPath = indexHTMLFilePath;
152  searchFlag = NO;
153  }
154  }
155  }
156  }
157 
158  return indexPath;
159 }
160 
161 
162 +(NSString*)searchForIndexHTMLFile:(NSMutableArray*)resources atIndex:(int)curIndex usingManager:(NSFileManager*)fileManager error:(NSError**)error{
163  NSString *resource = resources[curIndex][1];
164  NSString *directoryPath = resources[curIndex][0];
165  NSString *resourcePath = [directoryPath stringByAppendingPathComponent:resource];
166  NSError *anError = nil;
167  if ([resource.lowercaseString isEqualToString:@"index.html"]) {
168  return resourcePath;
169  }
170  else{
171  BOOL isDirectoryType = NO;
172  [fileManager fileExistsAtPath:resourcePath isDirectory:&isDirectoryType];
173  if (isDirectoryType) {
174  NSArray *resourceList = [fileManager contentsOfDirectoryAtPath:resourcePath error:&anError];
175  if (anError != nil) {
176  *error = anError;
177  return nil;
178  }
179  for (NSString *childResource in resourceList) {
180  [resources addObject:@[resourcePath, childResource]];
181  }
182  }
183  }
184  return [WebMover searchForIndexHTMLFile:resources atIndex:curIndex + 1 usingManager:fileManager error:error];
185 }
186 @end
NSString * moveDirectoriesSearchingForIndexHTML:error:(BOOL searchFlag, [error] NSError **error)
Definition: WebMover.m:102
NSString * searchForIndexHTMLFile:atIndex:usingManager:error:(NSMutableArray *resources, [atIndex] int curIndex, [usingManager] NSFileManager *fileManager, [error] NSError **error)
Definition: WebMover.m:162
NSString * moveWebFiles:error:(NSArray *movableFileTypes, [error] NSError **error)
Definition: WebMover.m:57