PageRenderTime 68ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/1.1.0/AppLimit.CloudComputing.SharpBox/StorageProvider/CIFS/Logic/CIFSStorageProviderService.cs

#
C# | 289 lines | 192 code | 52 blank | 45 comment | 30 complexity | 6e2f5c2168de9ff83c1369c07d983f6f MD5 | raw file
Possible License(s): MIT
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using AppLimit.CloudComputing.SharpBox.Common.IO;
  9. using AppLimit.CloudComputing.SharpBox.StorageProvider.API;
  10. using AppLimit.CloudComputing.SharpBox.StorageProvider.BaseObjects;
  11. using AppLimit.CloudComputing.SharpBox.Exceptions;
  12. namespace AppLimit.CloudComputing.SharpBox.StorageProvider.CIFS.Logic
  13. {
  14. internal class CIFSStorageProviderService : GenericStorageProviderService
  15. {
  16. public override bool VerifyCredentialType(ICloudStorageCredentials credentials)
  17. {
  18. return (credentials is ICredentials);
  19. }
  20. public override IStorageProviderSession CreateSession(ICloudStorageCredentials credentials, ICloudStorageConfiguration configuration)
  21. {
  22. // check if url available
  23. if (!Directory.Exists(configuration.ServiceLocator.LocalPath))
  24. {
  25. try
  26. {
  27. Directory.CreateDirectory(configuration.ServiceLocator.LocalPath);
  28. }
  29. catch(Exception)
  30. {
  31. return null;
  32. }
  33. }
  34. return new CIFSStorageProviderSession(credentials as ICloudStorageAccessToken, configuration as CIFSConfiguration, this);
  35. }
  36. public override ICloudFileSystemEntry RequestResource(IStorageProviderSession session, string Name, ICloudDirectoryEntry parent)
  37. {
  38. // build path
  39. String path;
  40. if (Name.Equals("/"))
  41. path = session.ServiceConfiguration.ServiceLocator.LocalPath;
  42. else if (parent == null)
  43. path = Path.Combine(path = session.ServiceConfiguration.ServiceLocator.LocalPath, Name);
  44. else
  45. path = new Uri(GetResourceUrl(session, parent, Name)).LocalPath;
  46. // check if file exists
  47. if (File.Exists(path))
  48. {
  49. // create the fileinfo
  50. FileInfo fInfo = new FileInfo(path);
  51. BaseFileEntry bf = new BaseFileEntry(fInfo.Name, fInfo.Length, fInfo.LastWriteTimeUtc, this, session) { Parent = parent };
  52. // add to parent
  53. if (parent != null)
  54. (parent as BaseDirectoryEntry).AddChild(bf);
  55. // go ahead
  56. return bf;
  57. }
  58. // check if directory exists
  59. else if (Directory.Exists(path))
  60. {
  61. // build directory info
  62. DirectoryInfo dInfo = new DirectoryInfo(path);
  63. // build bas dir
  64. BaseDirectoryEntry dir = CreateEntryByFileSystemInfo(dInfo, session, parent) as BaseDirectoryEntry;
  65. if (Name.Equals("/"))
  66. dir.Name = "/";
  67. // add to parent
  68. if (parent != null)
  69. (parent as BaseDirectoryEntry).AddChild(dir);
  70. // refresh the childs
  71. RefreshChildsOfDirectory(session, dir);
  72. // go ahead
  73. return dir;
  74. }
  75. else
  76. return null;
  77. }
  78. public override void RefreshResource(IStorageProviderSession session, ICloudFileSystemEntry resource)
  79. {
  80. // nothing to do for files
  81. if (!(resource is ICloudDirectoryEntry))
  82. return;
  83. // Refresh schild
  84. RefreshChildsOfDirectory(session, resource as BaseDirectoryEntry);
  85. }
  86. public override bool DeleteResource(IStorageProviderSession session, ICloudFileSystemEntry entry)
  87. {
  88. // generate the loca path
  89. String uriPath = GetResourceUrl(session, entry, null);
  90. Uri uri = new Uri(uriPath);
  91. // removed the file
  92. if (File.Exists(uri.LocalPath))
  93. File.Delete(uri.LocalPath);
  94. else if (Directory.Exists(uri.LocalPath))
  95. Directory.Delete(uri.LocalPath, true);
  96. // go ahead
  97. return true;
  98. }
  99. public override bool MoveResource(IStorageProviderSession session, ICloudFileSystemEntry fsentry, ICloudDirectoryEntry newParent)
  100. {
  101. // build the new uri
  102. String newPlace = GetResourceUrl(session, newParent, fsentry.Name);
  103. if (RenameResourceEx(session, fsentry, newPlace))
  104. {
  105. // remove from parent
  106. (fsentry.Parent as BaseDirectoryEntry).RemoveChild(fsentry as BaseFileEntry);
  107. // add to new parent
  108. (newParent as BaseDirectoryEntry).AddChild(fsentry as BaseFileEntry);
  109. return true;
  110. }
  111. else
  112. return false;
  113. }
  114. public override Stream CreateDownloadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry)
  115. {
  116. // get the full path
  117. String uriPath = GetResourceUrl(session, fileSystemEntry, null);
  118. Uri uri = new Uri(uriPath);
  119. // open src file
  120. return new FileStream(uri.LocalPath, FileMode.Open);
  121. }
  122. public override Stream CreateUploadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, long uploadSize)
  123. {
  124. // get the full path
  125. String uriPath = GetResourceUrl(session, fileSystemEntry, null);
  126. Uri uri = new Uri(uriPath);
  127. // set the new size
  128. BaseFileEntry f = fileSystemEntry as BaseFileEntry;
  129. f.Length = uploadSize;
  130. // create the file if not exists
  131. FileStream fs = null;
  132. if (!File.Exists(uri.LocalPath))
  133. fs = File.Create(uri.LocalPath);
  134. else
  135. fs = File.OpenWrite(uri.LocalPath);
  136. // go ahead
  137. return fs;
  138. }
  139. public override ICloudFileSystemEntry CreateResource(IStorageProviderSession session, string Name, ICloudDirectoryEntry parent)
  140. {
  141. // build the full url
  142. String resFull = GetResourceUrl(session, parent, Name);
  143. Uri uri = new Uri(resFull);
  144. // create the director
  145. DirectoryInfo dinfo = Directory.CreateDirectory(uri.LocalPath);
  146. // create the filesystem object
  147. ICloudFileSystemEntry fsEntry = CreateEntryByFileSystemInfo(dinfo, session, parent);
  148. // add parent child
  149. if (parent != null)
  150. (parent as BaseDirectoryEntry).AddChild(fsEntry as BaseFileEntry);
  151. // go ahead
  152. return fsEntry;
  153. }
  154. public override bool RenameResource(IStorageProviderSession session, ICloudFileSystemEntry fsentry, string newName)
  155. {
  156. // get new name uri
  157. String uriPath = GetResourceUrl(session, fsentry.Parent, newName);
  158. // do it
  159. return RenameResourceEx(session, fsentry, uriPath.ToString());
  160. }
  161. #region Helper
  162. private BaseFileEntry CreateEntryByFileSystemInfo(FileSystemInfo info, IStorageProviderSession session, ICloudDirectoryEntry parent)
  163. {
  164. if (info is DirectoryInfo)
  165. return new BaseDirectoryEntry(info.Name, 0, info.LastWriteTimeUtc, this, session);
  166. else if (info is FileInfo)
  167. return new BaseFileEntry(info.Name, (info as FileInfo).Length, info.LastWriteTimeUtc, this, session);
  168. else
  169. throw new Exception("Invalid filesysteminfo type");
  170. }
  171. private void RefreshChildsOfDirectory(IStorageProviderSession session, BaseDirectoryEntry dir)
  172. {
  173. // get the location
  174. String reslocation = GetResourceUrl(session, dir as ICloudFileSystemEntry, null);
  175. // ensure that we have a trailing slash
  176. reslocation = reslocation.TrimEnd('/');
  177. reslocation = reslocation + "/";
  178. // build the uri
  179. Uri resUri = new Uri(reslocation);
  180. // convert BaseDir to DirInfo
  181. DirectoryInfo dInfo = new DirectoryInfo(resUri.LocalPath);
  182. // clear childs
  183. dir.ClearChilds();
  184. // get all childs
  185. foreach (FileSystemInfo fInfo in dInfo.GetFileSystemInfos())
  186. {
  187. BaseFileEntry f = CreateEntryByFileSystemInfo(fInfo, session, dir);
  188. dir.AddChild(f);
  189. }
  190. }
  191. public bool RenameResourceEx(IStorageProviderSession session, ICloudFileSystemEntry fsentry, String newFullPath)
  192. {
  193. // get the uri
  194. String uriPath = GetResourceUrl(session, fsentry, null);
  195. Uri srUri = new Uri(uriPath);
  196. // get new name uri
  197. Uri tgUri = new Uri(newFullPath);
  198. // rename
  199. FileSystemInfo f = null;
  200. if (File.Exists(srUri.LocalPath))
  201. {
  202. f = new FileInfo(srUri.LocalPath);
  203. ((FileInfo)f).MoveTo(tgUri.LocalPath);
  204. }
  205. else if (Directory.Exists(srUri.LocalPath))
  206. {
  207. f = new DirectoryInfo(srUri.LocalPath);
  208. ((DirectoryInfo)f).MoveTo(tgUri.LocalPath);
  209. }
  210. else
  211. {
  212. throw new SharpBoxException(SharpBoxErrorCodes.ErrorFileNotFound);
  213. }
  214. // reload file info
  215. if (File.Exists(tgUri.LocalPath))
  216. {
  217. f = new FileInfo(tgUri.LocalPath);
  218. }
  219. else if (Directory.Exists(tgUri.LocalPath))
  220. {
  221. f = new DirectoryInfo(tgUri.LocalPath);
  222. }
  223. else
  224. {
  225. throw new SharpBoxException(SharpBoxErrorCodes.ErrorFileNotFound);
  226. }
  227. // update fsEntry
  228. BaseFileEntry fs = fsentry as BaseFileEntry;
  229. fs.Name = Path.GetFileName(tgUri.LocalPath);
  230. fs.Length = (f is FileInfo ? (f as FileInfo).Length : 0);
  231. fs.Modified = f.LastWriteTimeUtc;
  232. // go ahead
  233. return true;
  234. }
  235. #endregion
  236. }
  237. }