BadScript 2
Loading...
Searching...
No Matches
BadOpenKmFileSystem.cs
Go to the documentation of this file.
2
3using ewu.adam.openkm.rest;
4using ewu.adam.openkm.rest.Bean;
5
10
15{
19 private readonly IOkmWebservice m_Webservice;
20
24 private Folder m_Current;
25
29 private string? m_StartupDirectory;
30
35 public BadOpenKmFileSystem(IOkmWebservice webService)
36 {
37 m_Webservice = webService;
38
39 m_Current = m_Webservice.GetFolderProperties(GetStartupDirectory())
40 .Result;
41 }
42
49 public BadOpenKmFileSystem(string url, string user, string password) :
50 this(OkmWebserviceFactory.NewInstance(url, user, password)) { }
51
52#region IFileSystem Members
53
55 public string GetStartupDirectory()
56 {
57 return m_StartupDirectory ??= m_Webservice.GetPersonalFolder()
58 .Result.path;
59 }
60
62 public bool Exists(string path)
63 {
64 return m_Webservice.HasNode(MakeFullPath(path))
65 .Result;
66 }
67
69 public bool IsFile(string path)
70 {
71 return m_Webservice.IsValidDocument(MakeFullPath(path))
72 .Result;
73 }
74
76 public bool IsDirectory(string path)
77 {
78 return m_Webservice.IsValidFolder(MakeFullPath(path))
79 .Result;
80 }
81
83 public IEnumerable<string> GetFiles(string path, string extension, bool recursive)
84 {
85 path = MakeFullPath(path);
86
87 return recursive ? InnerGetFilesRecursive(path, extension) : InnerGetFiles(path, extension);
88 }
89
91 public IEnumerable<string> GetDirectories(string path, bool recursive)
92 {
93 path = MakeFullPath(path);
94
95 return recursive ? InnerGetDirectoriesRecursive(path) : InnerGetDirectories(path);
96 }
97
99 public void CreateDirectory(string path, bool recursive = false)
100 {
101 path = MakeFullPath(path);
102
103 if (recursive)
104 {
106 }
107 else
108 {
110 }
111 }
112
114 public void DeleteDirectory(string path, bool recursive)
115 {
116 path = MakeFullPath(path);
117
118 if (!Exists(path))
119 {
120 throw new Exception("Directory does not exist.");
121 }
122
123 if (!recursive && HasChildren(path))
124 {
125 throw new Exception("Directory is not empty.");
126 }
127
128 m_Webservice.DeleteFolder(path)
129 .Wait();
130 }
131
133 public void DeleteFile(string path)
134 {
135 path = MakeFullPath(path);
136
137 if (!IsFile(path))
138 {
139 throw new Exception("File does not exist.");
140 }
141
142 m_Webservice.DeleteDocument(path)
143 .Wait();
144 }
145
147 public string GetFullPath(string path)
148 {
149 return MakeFullPath(path);
150 }
151
153 public Stream OpenRead(string path)
154 {
155 Stream result = m_Webservice.GetContent(MakeFullPath(path))
156 .Result;
157
158 return result;
159 }
160
162 public Stream OpenWrite(string path, BadWriteMode mode)
163 {
164 return new BadOpenKmWritableStream(m_Webservice, MakeFullPath(path), mode);
165 }
166
168 public string GetCurrentDirectory()
169 {
170 return m_Current.path;
171 }
172
174 public void SetCurrentDirectory(string path)
175 {
176 m_Current = m_Webservice.GetFolderProperties(MakeFullPath(path))
177 .Result;
178 }
179
181 public void Copy(string src, string dst, bool overwrite = true)
182 {
183 src = MakeFullPath(src);
184 dst = MakeFullPath(dst);
185
186 if (!Exists(src))
187 {
188 throw new Exception("Source does not exist");
189 }
190
191 if (IsDirectory(src))
192 {
193 throw new
194 NotSupportedException("At the moment, copying directories is not supported. (need to implement recursive move files)"
195 );
196 }
197
198 if (!Exists(dst))
199 {
200 throw new Exception("Target directory does not exist");
201 }
202
203 string dstFile = MakeFullPath(Path.Combine(dst, Path.GetFileName(src)));
204
205 if (Exists(dstFile))
206 {
207 if (!IsFile(dstFile))
208 {
209 throw new Exception("Target file is a directory");
210 }
211
212 if (!overwrite)
213 {
214 throw new Exception("Target already exists");
215 }
216
217 m_Webservice.DeleteDocument(dstFile)
218 .Wait();
219 }
220
221 m_Webservice.CopyDocument(src, dst)
222 .Wait();
223 }
224
226 public void Move(string src, string dst, bool overwrite = true)
227 {
228 src = MakeFullPath(src);
229 dst = MakeFullPath(dst);
230
231 if (!Exists(src))
232 {
233 throw new Exception("Source does not exist");
234 }
235
236 if (IsDirectory(src))
237 {
238 throw new
239 NotSupportedException("At the moment, moving directories is not supported. (need to implement recursive move files)"
240 );
241 }
242
243 if (!Exists(dst))
244 {
245 throw new Exception("Target directory does not exist");
246 }
247
248 string dstFile = MakeFullPath(Path.Combine(dst, Path.GetFileName(src)));
249
250 if (Exists(dstFile))
251 {
252 if (!IsFile(dstFile))
253 {
254 throw new Exception("Target file is a directory");
255 }
256
257 if (!overwrite)
258 {
259 throw new Exception("Target already exists");
260 }
261
262 m_Webservice.DeleteDocument(dstFile)
263 .Wait();
264 }
265
266 m_Webservice.MoveDocument(src, dst)
267 .Wait();
268 }
269
270#endregion
271
277 private string MakeFullPath(string path)
278 {
280
281 return full;
282 }
283
289 private bool HasChildren(string path)
290 {
291 return m_Webservice.GetFolderChildren(path)
292 .Result.folder.Count >
293 0 ||
294 m_Webservice.GetDocumentChildren(path)
295 .Result.document.Count >
296 0;
297 }
298
304 private IEnumerable<string> InnerGetDirectories(string path)
305 {
306 FolderList? current = m_Webservice.GetFolderChildren(path)
307 .Result;
308
309 return current == null ? Enumerable.Empty<string>() : current.folder.Select(x => x.path);
310 }
311
318 private IEnumerable<string> InnerGetFiles(string path, string extension)
319 {
320 DocumentList? current = m_Webservice.GetDocumentChildren(path)
321 .Result;
322
323 if (current == null)
324 {
325 yield break;
326 }
327
328 foreach (Document document in current.document)
329 {
330 string ext = Path.GetExtension(document.path);
331
332 if (string.IsNullOrEmpty(extension) || ext == extension)
333 {
334 yield return document.path;
335 }
336 }
337 }
338
344 private IEnumerable<string> InnerGetDirectoriesRecursive(string path)
345 {
346 foreach (string dir in InnerGetDirectories(path))
347 {
348 yield return dir;
349
350 foreach (string innerDir in InnerGetDirectoriesRecursive(dir))
351 {
352 yield return innerDir;
353 }
354 }
355 }
356
363 private IEnumerable<string> InnerGetFilesRecursive(string path, string extension)
364 {
365 foreach (string file in InnerGetFiles(path, extension))
366 {
367 yield return file;
368 }
369
370 foreach (string directory in InnerGetDirectories(path))
371 {
372 foreach (string file in InnerGetFilesRecursive(directory, extension))
373 {
374 yield return file;
375 }
376 }
377 }
378
379
384 private void InnerCreateDirectoryRecursive(string path)
385 {
386 if (IsDirectory(path))
387 {
388 return;
389 }
390
391 string? parent = Path.GetDirectoryName(path);
392
393 if (parent != null)
394 {
396 }
397
399 }
400
405 private void InnerCreateDirectory(string path)
406 {
407 m_Webservice.CreateFolderSimple(path)
408 .Wait();
409 }
410}
Implements a FileSystem for OpenKM.
string GetStartupDirectory()
The Startup Directory of the Application.The Startup Directory of the Application
void Move(string src, string dst, bool overwrite=true)
Moves a file or directory to a new location.
IEnumerable< string > InnerGetFiles(string path, string extension)
Returns all files in the given directory that match the specified extension.
IEnumerable< string > InnerGetDirectories(string path)
Returns all directories in the given directory.
BadOpenKmFileSystem(string url, string user, string password)
Constructs a new BadOpenKmFileSystem instance.
void InnerCreateDirectory(string path)
Creates a directory.
bool IsDirectory(string path)
Returns true if the given path is a directory.true if the given path is a directory
bool HasChildren(string path)
Returns true if the given path has children.
bool IsFile(string path)
Returns true if the given path is a file.true if the given path is a file
IEnumerable< string > GetDirectories(string path, bool recursive)
Returns all directories in the given directory.Enumeration of all directories in the given directory ...
IEnumerable< string > InnerGetFilesRecursive(string path, string extension)
Recursively returns all files in the given directory that match the specified extension.
readonly IOkmWebservice m_Webservice
The OpenKM Webservice.
void InnerCreateDirectoryRecursive(string path)
Creates a directory recursively.
void CreateDirectory(string path, bool recursive=false)
Creates a new directory.
string GetFullPath(string path)
Returns the full path of the given path.Full Path
string? m_StartupDirectory
The Startup Directory.
Stream OpenWrite(string path, BadWriteMode mode)
Opens a file for writing.File Stream
void DeleteDirectory(string path, bool recursive)
Deletes a directory.
bool Exists(string path)
Returns true if the given path is a file or directory.true if the given path is a file or directory
IEnumerable< string > GetFiles(string path, string extension, bool recursive)
Returns all files in the given directory that match the specified extension.Enumeration of all files ...
string GetCurrentDirectory()
Returns the Current Directory.The Current Directory
Stream OpenRead(string path)
Opens a file for reading.File Stream
void Copy(string src, string dst, bool overwrite=true)
Copies a file or directory to a new location.
void SetCurrentDirectory(string path)
Sets the current Directory.
void DeleteFile(string path)
Deletes a file.
BadOpenKmFileSystem(IOkmWebservice webService)
Constructs a new BadOpenKmFileSystem instance.
IEnumerable< string > InnerGetDirectoriesRecursive(string path)
Recursively returns all directories in the given directory.
string MakeFullPath(string path)
Makes the given path a full path.
Implements a Stream for writing to OpenKM.
Implements Operations to read and manipulate File System Paths.
static string ResolvePath(string path, string currentDir)
Defines the interface for a file system.
Definition IFileSystem.cs:7
File System Implementation for OpenKM.
Contains the Implementation of the BadScript Virtual File System.
BadWriteMode
The Write Modes of the File System Abstraction.