6
6
import java .nio .file .Files ;
7
7
import java .nio .file .StandardCopyOption ;
8
8
9
+ import Cataphract .API .Anvil ;
9
10
import Cataphract .API .IOStreams ;
10
11
import Cataphract .API .Minotaur .Cryptography ;
11
12
12
13
public class FileManagement
13
14
{
14
15
private String _username = "" ;
15
16
private String _name = "" ;
16
- private String _defaultPath = "" ;
17
17
private String _presentWorkingDirectory = "" ;
18
18
19
19
private Console console = System .console ();
@@ -22,7 +22,7 @@ public FileManagement(String username)throws Exception
22
22
{
23
23
_username = username ;
24
24
_name = new Cataphract .API .Dragon .Login (username ).getNameLogic ();
25
- _defaultPath = "./Users/Cataphract/" + _username + "/ " ;
25
+ _presentWorkingDirectory = "./Users/Cataphract/" + _username + "/" ;
26
26
}
27
27
28
28
/*****************************************
@@ -31,8 +31,8 @@ public FileManagement(String username)throws Exception
31
31
32
32
private final boolean login ()throws Exception
33
33
{
34
- IOStreams .println ("Username: " + _username );
35
- return new Cataphract .API .Dragon .Login (_username ).authenticationLogic (Cryptography .stringToSHA3_256 (console .readLine ( " Password: " )), Cryptography .stringToSHA3_256 (console .readLine ( " Security Key: " )));
34
+ IOStreams .println ("> Username: " + _name );
35
+ return new Cataphract .API .Dragon .Login (_username ).authenticationLogic (Cryptography .stringToSHA3_256 (String . valueOf ( console .readPassword ( "> Password: " ))) , Cryptography .stringToSHA3_256 (String . valueOf ( console .readPassword ( "> Security Key: " )))) ;
36
36
}
37
37
38
38
/*****************************************
@@ -44,27 +44,43 @@ private boolean checkEntityExistence(String fileName)throws Exception
44
44
return new File (fileName ).exists ();
45
45
}
46
46
47
- private void deleteDirectoryFile (String fileName )throws Exception
47
+ private final void deleteEntity (String delFile )throws Exception
48
48
{
49
- fileName = _defaultPath + _presentWorkingDirectory + fileName ;
50
- File deletionEntity = new File (fileName );
51
-
52
- if (! checkEntityExistence (fileName ))
49
+ try
53
50
{
54
- IOStreams .printError ("The specified file or directory does not exist." );
51
+ delFile = _presentWorkingDirectory +delFile ;
52
+ if (checkEntityExistence (delFile ))
53
+ {
54
+ File f =new File (delFile );
55
+ if (f .isDirectory ())
56
+ deleteEntityHelper (f );
57
+ else
58
+ f .delete ();
59
+ }
60
+ else
61
+ IOStreams .printError ("The Specified File/Directory Does Not Exist." );
62
+ System .gc ();
55
63
}
56
- else
64
+ catch (Exception E )
65
+ {
66
+ //troubleshooting tips here
67
+ E .printStackTrace ();
68
+ }
69
+ }
70
+
71
+ private final void deleteEntityHelper (File delfile )throws Exception
72
+ {
73
+ if (delfile .listFiles () != null )
57
74
{
58
- if (deletionEntity .isDirectory ())
59
- for (File filesInDirectory : deletionEntity .listFiles ())
60
- deleteDirectoryFile (filesInDirectory .getName ());
61
- deletionEntity .delete ();
75
+ for (File fileInDirectory : delfile .listFiles ())
76
+ deleteEntityHelper (fileInDirectory );
62
77
}
78
+ delfile .delete ();
63
79
}
64
80
65
81
private void viewDirectoryTree ()throws Exception
66
82
{
67
- File treeView = new File (_defaultPath + _presentWorkingDirectory );
83
+ File treeView = new File (_presentWorkingDirectory );
68
84
IOStreams .println ("\n --- [ TREE VIEW ] ---\n " );
69
85
viewDirTreeHelper (0 , treeView );
70
86
IOStreams .println ("" );
@@ -74,9 +90,9 @@ private final void viewDirTreeHelper(int indent, File file) {
74
90
System .out .print ("|" );
75
91
76
92
for (int i = 0 ; i < indent ; ++i )
77
- System . out . print ('-' );
93
+ IOStreams . print ("-" );
78
94
79
- System . out . println (file .getName ().replace (_username , _name + " [ USER ROOT DIRECTORY ]" ));
95
+ IOStreams . println (file .getName ().replace (_username , _name + " [ USER HOME DIRECTORY ]" ));
80
96
81
97
if (file .isDirectory ())
82
98
{
@@ -88,12 +104,9 @@ private final void viewDirTreeHelper(int indent, File file) {
88
104
89
105
private final void navPreviousDirectory ()throws Exception
90
106
{
91
- int lastSlashIndex = _presentWorkingDirectory .lastIndexOf ('/' );
92
- if (lastSlashIndex != -1 )
93
- {
94
- _presentWorkingDirectory = _presentWorkingDirectory .substring (0 , lastSlashIndex );
95
- }
96
- else
107
+ _presentWorkingDirectory = _presentWorkingDirectory .substring (0 , _presentWorkingDirectory .length () - 1 );
108
+ _presentWorkingDirectory = _presentWorkingDirectory .replace (_presentWorkingDirectory .substring (_presentWorkingDirectory .lastIndexOf ('/' ), _presentWorkingDirectory .length ()), "/" );
109
+ if (_presentWorkingDirectory .equals ("./Users/Cataphract/" ))
97
110
{
98
111
IOStreams .printError ("Permission Denied." );
99
112
resetToHomeDirectory ();
@@ -102,18 +115,18 @@ private final void navPreviousDirectory()throws Exception
102
115
103
116
private final void resetToHomeDirectory ()
104
117
{
105
- _presentWorkingDirectory = "/" ;
118
+ _presentWorkingDirectory = "./Users/Cataphract/" + _username + '/' ;
106
119
}
107
120
108
121
private final void makeDirectory (String fileName ) throws Exception
109
122
{
110
- new File (_defaultPath + _presentWorkingDirectory + fileName ).mkdirs ();
123
+ new File (_presentWorkingDirectory + fileName ).mkdirs ();
111
124
}
112
125
113
126
private final void renameEntity (String fileName , String newFileName ) throws Exception
114
127
{
115
- fileName = _defaultPath + _presentWorkingDirectory + fileName ;
116
- newFileName = _defaultPath + _presentWorkingDirectory + newFileName ;
128
+ fileName = _presentWorkingDirectory + fileName ;
129
+ newFileName = _presentWorkingDirectory + newFileName ;
117
130
118
131
if (checkEntityExistence (newFileName ))
119
132
new File (fileName ).renameTo (new File (newFileName ));
@@ -126,7 +139,7 @@ private final void copyMoveEntity(String fileName, String destination, boolean m
126
139
if (!checkEntityExistence (fileName ) && !checkEntityExistence (destination ))
127
140
IOStreams .printError ("Invalid file name or destination. Permission Denied." );
128
141
129
- copyMoveHelper (new File (_defaultPath + _presentWorkingDirectory + fileName ), new File (_defaultPath + _presentWorkingDirectory + destination ), move );
142
+ copyMoveHelper (new File (_presentWorkingDirectory + fileName ), new File (_presentWorkingDirectory + destination ), move );
130
143
}
131
144
132
145
private final void copyMoveHelper (File source , File destination , boolean move )throws Exception
@@ -150,6 +163,49 @@ private final void copyMoveHelper(File source, File destination, boolean move)th
150
163
}
151
164
}
152
165
166
+ private final void listEntities ()throws Exception
167
+ {
168
+ //String format = "%1$-60s|%2$-50s|%3$-20s\n";
169
+ String format = "%1$-32s| %2$-24s| %3$-10s\n " ;
170
+ String c = "-" ;
171
+ if (checkEntityExistence (_presentWorkingDirectory ))
172
+ {
173
+ File dPath =new File (_presentWorkingDirectory );
174
+ System .out .println ("\n " );
175
+ String disp = (String .format (format , "Directory/File Name" , "File Size [In KB]" ,"Type" ));
176
+ System .out .println (disp + c .repeat (disp .length ()) + "\n " );
177
+ for (File file : dPath .listFiles ())
178
+ {
179
+ //System.out.format(String.format(format, file.getPath().replace(User,Name), file.getName().replace(User,Name), file.length()/1024+" KB"));
180
+ System .out .format (String .format (format , file .getName ().replace (_username , _name ), file .length ()/1024 +" KB" , file .isDirectory ()?"Directory" :"File" ));
181
+ }
182
+ System .out .println ();
183
+ }
184
+ else
185
+ IOStreams .printError ("The Specified File/Directory Does Not Exist." );
186
+ System .gc ();
187
+ }
188
+
189
+ private final void changeDirectory (String destination )throws Exception
190
+ {
191
+ if (destination .equals (".." ))
192
+ {
193
+ navPreviousDirectory ();
194
+ System .gc ();
195
+ }
196
+ else
197
+ {
198
+ if (checkEntityExistence (_presentWorkingDirectory + destination ))
199
+ {
200
+ _presentWorkingDirectory = _presentWorkingDirectory + destination + "/" ;
201
+ }
202
+ else
203
+ {
204
+ IOStreams .printError ("\' " + destination + "\' does not exist" );
205
+ }
206
+ }
207
+ }
208
+
153
209
/*****************************************
154
210
* GRINCH FILE MANAGEMENT & SCRIPT LOGIC *
155
211
*****************************************/
@@ -200,7 +256,7 @@ private void grinchInterpreter(String command)throws Exception
200
256
if (commandArray .length < 2 )
201
257
IOStreams .printError ("Invalid Syntax." );
202
258
else
203
- deleteDirectoryFile (commandArray [1 ]);
259
+ deleteEntity (commandArray [1 ]);
204
260
break ;
205
261
206
262
case "rename" :
@@ -221,10 +277,18 @@ private void grinchInterpreter(String command)throws Exception
221
277
break ;
222
278
223
279
case "pwd" :
224
- IOStreams .println (_defaultPath + _presentWorkingDirectory );
280
+ IOStreams .println (( _presentWorkingDirectory ). replace ( _username , _name ) );
225
281
break ;
226
282
227
283
case "cd" :
284
+ if (commandArray .length < 2 )
285
+ IOStreams .printError ("Invalid Syntax." );
286
+ else
287
+ changeDirectory (commandArray [1 ]);
288
+ break ;
289
+
290
+ case "cd.." :
291
+ navPreviousDirectory ();
228
292
break ;
229
293
230
294
case "tree" :
@@ -233,17 +297,22 @@ private void grinchInterpreter(String command)throws Exception
233
297
234
298
case "dir" :
235
299
case "ls" :
300
+ listEntities ();
236
301
break ;
237
302
238
303
case "download" :
239
304
break ;
240
305
306
+ case "home" :
307
+ resetToHomeDirectory ();
308
+ break ;
309
+
241
310
case "exit" :
242
311
case "" :
243
312
break ;
244
313
245
314
default :
246
- IOStreams . printError ( "Command Not Found." );
315
+ Anvil . anvilInterpreter ( commandArray );
247
316
}
248
317
}
249
318
0 commit comments