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 m_Current = m_Webservice.GetFolderProperties(GetStartupDirectory()).Result;
39 }
40
47 public BadOpenKmFileSystem(string url, string user, string password) : this(
48 OkmWebserviceFactory.NewInstance(url, user, password)
49 ) { }
50
51
53 public string GetStartupDirectory()
54 {
55 return m_StartupDirectory ??= m_Webservice.GetPersonalFolder().Result.path;
56 }
57
59 public bool Exists(string path)
60 {
61 return m_Webservice.HasNode(MakeFullPath(path)).Result;
62 }
63
65 public bool IsFile(string path)
66 {
67 return m_Webservice.IsValidDocument(MakeFullPath(path)).Result;
68 }
69
71 public bool IsDirectory(string path)
72 {
73 return m_Webservice.IsValidFolder(MakeFullPath(path)).Result;
74 }
75
77 public IEnumerable<string> GetFiles(string path, string extension, bool recursive)
78 {
79 path = MakeFullPath(path);
80
81 return recursive ? InnerGetFilesRecursive(path, extension) : InnerGetFiles(path, extension);
82 }
83
85 public IEnumerable<string> GetDirectories(string path, bool recursive)
86 {
87 path = MakeFullPath(path);
88
89 return recursive ? InnerGetDirectoriesRecursive(path) : InnerGetDirectories(path);
90 }
91
93 public void CreateDirectory(string path, bool recursive = false)
94 {
95 path = MakeFullPath(path);
96
97 if (recursive)
98 {
100 }
101 else
102 {
104 }
105 }
106
108 public void DeleteDirectory(string path, bool recursive)
109 {
110 path = MakeFullPath(path);
111
112 if (!Exists(path))
113 {
114 throw new Exception("Directory does not exist.");
115 }
116
117 if (!recursive && HasChildren(path))
118 {
119 throw new Exception("Directory is not empty.");
120 }
121
122 m_Webservice.DeleteFolder(path).Wait();
123 }
124
126 public void DeleteFile(string path)
127 {
128 path = MakeFullPath(path);
129
130 if (!IsFile(path))
131 {
132 throw new Exception("File does not exist.");
133 }
134
135 m_Webservice.DeleteDocument(path).Wait();
136 }
137
139 public string GetFullPath(string path)
140 {
141 return MakeFullPath(path);
142 }
143
145 public Stream OpenRead(string path)
146 {
147 Stream result = m_Webservice.GetContent(MakeFullPath(path)).Result;
148
149 return result;
150 }
151
153 public Stream OpenWrite(string path, BadWriteMode mode)
154 {
155 return new BadOpenKmWritableStream(m_Webservice, MakeFullPath(path), mode);
156 }
157
159 public string GetCurrentDirectory()
160 {
161 return m_Current.path;
162 }
163
165 public void SetCurrentDirectory(string path)
166 {
167 m_Current = m_Webservice.GetFolderProperties(MakeFullPath(path)).Result;
168 }
169
171 public void Copy(string src, string dst, bool overwrite = true)
172 {
173 src = MakeFullPath(src);
174 dst = MakeFullPath(dst);
175
176 if (!Exists(src))
177 {
178 throw new Exception("Source does not exist");
179 }
180
181 if (IsDirectory(src))
182 {
183 throw new NotSupportedException(
184 "At the moment, copying directories is not supported. (need to implement recursive move files)"
185 );
186 }
187
188 if (!Exists(dst))
189 {
190 throw new Exception("Target directory does not exist");
191 }
192
193 string dstFile = MakeFullPath(Path.Combine(dst, Path.GetFileName(src)));
194
195 if (Exists(dstFile))
196 {
197 if (!IsFile(dstFile))
198 {
199 throw new Exception("Target file is a directory");
200 }
201
202 if (!overwrite)
203 {
204 throw new Exception("Target already exists");
205 }
206
207 m_Webservice.DeleteDocument(dstFile).Wait();
208 }
209
210 m_Webservice.CopyDocument(src, dst).Wait();
211 }
212
214 public void Move(string src, string dst, bool overwrite = true)
215 {
216 src = MakeFullPath(src);
217 dst = MakeFullPath(dst);
218
219 if (!Exists(src))
220 {
221 throw new Exception("Source does not exist");
222 }
223
224 if (IsDirectory(src))
225 {
226 throw new NotSupportedException(
227 "At the moment, moving directories is not supported. (need to implement recursive move files)"
228 );
229 }
230
231 if (!Exists(dst))
232 {
233 throw new Exception("Target directory does not exist");
234 }
235
236 string dstFile = MakeFullPath(Path.Combine(dst, Path.GetFileName(src)));
237
238 if (Exists(dstFile))
239 {
240 if (!IsFile(dstFile))
241 {
242 throw new Exception("Target file is a directory");
243 }
244
245 if (!overwrite)
246 {
247 throw new Exception("Target already exists");
248 }
249
250 m_Webservice.DeleteDocument(dstFile).Wait();
251 }
252
253 m_Webservice.MoveDocument(src, dst).Wait();
254 }
255
261 private string MakeFullPath(string path)
262 {
264
265 return full;
266 }
267
273 private bool HasChildren(string path)
274 {
275 return m_Webservice.GetFolderChildren(path).Result.folder.Count > 0 ||
276 m_Webservice.GetDocumentChildren(path).Result.document.Count > 0;
277 }
278
284 private IEnumerable<string> InnerGetDirectories(string path)
285 {
286 FolderList? current = m_Webservice.GetFolderChildren(path).Result;
287
288 return current == null ? Enumerable.Empty<string>() : current.folder.Select(x => x.path);
289 }
290
297 private IEnumerable<string> InnerGetFiles(string path, string extension)
298 {
299 DocumentList? current = m_Webservice.GetDocumentChildren(path).Result;
300
301 if (current == null)
302 {
303 yield break;
304 }
305
306 foreach (Document document in current.document)
307 {
308 string ext = Path.GetExtension(document.path);
309
310 if (string.IsNullOrEmpty(extension) || ext == extension)
311 {
312 yield return document.path;
313 }
314 }
315 }
316
322 private IEnumerable<string> InnerGetDirectoriesRecursive(string path)
323 {
324 foreach (string dir in InnerGetDirectories(path))
325 {
326 yield return dir;
327
328 foreach (string innerDir in InnerGetDirectoriesRecursive(dir))
329 {
330 yield return innerDir;
331 }
332 }
333 }
334
341 private IEnumerable<string> InnerGetFilesRecursive(string path, string extension)
342 {
343 foreach (string file in InnerGetFiles(path, extension))
344 {
345 yield return file;
346 }
347
348 foreach (string directory in InnerGetDirectories(path))
349 {
350 foreach (string file in InnerGetFilesRecursive(directory, extension))
351 {
352 yield return file;
353 }
354 }
355 }
356
357
362 private void InnerCreateDirectoryRecursive(string path)
363 {
364 if (IsDirectory(path))
365 {
366 return;
367 }
368
369 string? parent = Path.GetDirectoryName(path);
370
371 if (parent != null)
372 {
374 }
375
377 }
378
383 private void InnerCreateDirectory(string path)
384 {
385 m_Webservice.CreateFolderSimple(path).Wait();
386 }
387}
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.