PageRenderTime 80ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/redistributable/AppLimit.CloudComputing.SharpBox/StorageProvider/GenericStorageProvider.cs

https://gitlab.com/rekby-archive/onlyoffice-CommunityServer
C# | 305 lines | 138 code | 41 blank | 126 comment | 22 complexity | 93a7b6b086d6789cfb567d4ee3ec189b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using AppLimit.CloudComputing.SharpBox.Common.IO;
  5. using AppLimit.CloudComputing.SharpBox.Exceptions;
  6. using AppLimit.CloudComputing.SharpBox.StorageProvider.API;
  7. using AppLimit.CloudComputing.SharpBox.StorageProvider.BaseObjects;
  8. namespace AppLimit.CloudComputing.SharpBox.StorageProvider
  9. {
  10. /// <summary>
  11. /// The generic storage provider class implements platform independent logic as
  12. /// base for all managed storage provider. All platform specific logic has to be implemented
  13. /// in the storage provider service interface
  14. /// </summary>
  15. public class GenericStorageProvider : ICloudStorageProviderInternal
  16. {
  17. /// <summary>
  18. /// A specific implementation of a storage service interface which contains
  19. /// all provider specific logic
  20. /// </summary>
  21. protected IStorageProviderService Service;
  22. /// <summary>
  23. /// A provider specific implementation of a session
  24. /// </summary>
  25. protected IStorageProviderSession Session;
  26. /// <summary>
  27. /// The constructure need a specific service implementation
  28. /// </summary>
  29. /// <param name="service"></param>
  30. public GenericStorageProvider(IStorageProviderService service)
  31. {
  32. Service = service;
  33. }
  34. #region ICloudStorageProvider Members
  35. /// <summary>
  36. /// This method opens a session for the implemented storage provider based on an existing
  37. /// security token.
  38. /// </summary>
  39. /// <param name="configuration"></param>
  40. /// <param name="token"></param>
  41. /// <returns></returns>
  42. public ICloudStorageAccessToken Open(ICloudStorageConfiguration configuration, ICloudStorageAccessToken token)
  43. {
  44. // Verify the compatibility of the credentials
  45. if (!Service.VerifyAccessTokenType(token))
  46. throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidCredentialsOrConfiguration);
  47. // create a new session
  48. Session = Service.CreateSession(token, configuration);
  49. // check the session
  50. if (Session == null)
  51. throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidCredentialsOrConfiguration);
  52. // return the accesstoken token
  53. return Session.SessionToken;
  54. }
  55. /// <summary>
  56. /// This method closes the established session to a service provider
  57. /// </summary>
  58. public void Close()
  59. {
  60. // close the session
  61. Service.CloseSession(Session);
  62. // remove reference
  63. Session = null;
  64. }
  65. /// <summary>
  66. /// This methid returns the root node of the virtual filesystem which
  67. /// is abstracted by SharpBox
  68. /// </summary>
  69. /// <returns></returns>
  70. public ICloudDirectoryEntry GetRoot()
  71. {
  72. return Service.RequestResource(Session, "/", null) as ICloudDirectoryEntry;
  73. }
  74. /// <summary>
  75. /// This method returns a filesystem object, this can be files or folders
  76. /// </summary>
  77. /// <param name="path"></param>
  78. /// <param name="parent"></param>
  79. /// <returns></returns>
  80. public ICloudFileSystemEntry GetFileSystemObject(string path, ICloudDirectoryEntry parent)
  81. {
  82. /*
  83. * This section generates for every higher object the object tree
  84. */
  85. var ph = new PathHelper(path);
  86. var elements = ph.GetPathElements();
  87. // create the virtual root
  88. var current = parent ?? GetRoot();
  89. // build the root
  90. // check if we request only the root
  91. if (path.Equals("/"))
  92. return current;
  93. if (Service.SupportsDirectRetrieve)
  94. {
  95. //Request directly
  96. return Service.RequestResource(Session, path.TrimStart('/'), current);
  97. }
  98. // create the path tree
  99. for (var i = 0; i <= elements.Length - 1; i++)
  100. {
  101. var elem = elements[i];
  102. if (i == elements.Length - 1)
  103. {
  104. return current.GetChild(elem, false);
  105. }
  106. try
  107. {
  108. current = current.GetChild(elem, true) as ICloudDirectoryEntry;
  109. }
  110. catch (SharpBoxException e)
  111. {
  112. // if not found, create a virtual one
  113. if (e.ErrorCode == SharpBoxErrorCodes.ErrorFileNotFound)
  114. current = GenericStorageProviderFactory.CreateDirectoryEntry(Session, elem, current);
  115. else
  116. throw;
  117. }
  118. }
  119. // looks like an error
  120. return null;
  121. }
  122. /// <summary>
  123. /// This method creates a folder in a given parent folder.
  124. /// Override this if your storage support resources having same name in one folder.
  125. /// </summary>
  126. /// <param name="name"></param>
  127. /// <param name="parent"></param>
  128. /// <returns></returns>
  129. public virtual ICloudDirectoryEntry CreateFolder(string name, ICloudDirectoryEntry parent)
  130. {
  131. // solve the parent issue
  132. if (parent == null)
  133. {
  134. parent = GetRoot();
  135. if (parent == null)
  136. return null;
  137. }
  138. // Don't support resources having same name in one folder by default
  139. var child = parent.FirstOrDefault(x => x.Name.Equals(name) && x is ICloudDirectoryEntry);
  140. if (child != null)
  141. return child as ICloudDirectoryEntry;
  142. return Service.CreateResource(Session, name, parent) as ICloudDirectoryEntry;
  143. }
  144. /// <summary>
  145. /// This method removes a given filesystem object from the cloud storage
  146. /// </summary>
  147. /// <param name="fsentry"></param>
  148. /// <returns></returns>
  149. public bool DeleteFileSystemEntry(ICloudFileSystemEntry fsentry)
  150. {
  151. return Service.DeleteResource(Session, fsentry);
  152. }
  153. /// <summary>
  154. /// This method moves a specifc filesystem object from his current location
  155. /// into a new folder
  156. /// </summary>
  157. /// <param name="fsentry"></param>
  158. /// <param name="newParent"></param>
  159. /// <returns></returns>
  160. public bool MoveFileSystemEntry(ICloudFileSystemEntry fsentry, ICloudDirectoryEntry newParent)
  161. {
  162. return Service.MoveResource(Session, fsentry, newParent);
  163. }
  164. /// <summary>
  165. /// This method moves a specifc filesystem object from his current location
  166. /// into a new folder
  167. /// </summary>
  168. /// <param name="fsentry"></param>
  169. /// <param name="newParent"></param>
  170. /// <returns></returns>
  171. public bool CopyFileSystemEntry(ICloudFileSystemEntry fsentry, ICloudDirectoryEntry newParent)
  172. {
  173. return Service.CopyResource(Session, fsentry, newParent);
  174. }
  175. /// <summary>
  176. /// This method renames a given filesystem object (file or folder)
  177. /// </summary>
  178. /// <param name="fsentry"></param>
  179. /// <param name="newName"></param>
  180. /// <returns></returns>
  181. public bool RenameFileSystemEntry(ICloudFileSystemEntry fsentry, string newName)
  182. {
  183. // save the old name
  184. var renamedId = fsentry.Id;
  185. // rename the resource
  186. if (Service.RenameResource(Session, fsentry, newName))
  187. {
  188. // get the parent
  189. var p = fsentry.Parent as BaseDirectoryEntry;
  190. // remove the old childname
  191. p.RemoveChildById(renamedId);
  192. // readd the child
  193. p.AddChild(fsentry as BaseFileEntry);
  194. // go ahead
  195. return true;
  196. }
  197. return false;
  198. }
  199. /// <summary>
  200. /// This method creates a file in the cloud storage
  201. /// </summary>
  202. /// <param name="parent"></param>
  203. /// <param name="name"></param>
  204. /// <returns></returns>
  205. public virtual ICloudFileSystemEntry CreateFile(ICloudDirectoryEntry parent, string name)
  206. {
  207. // build the parent
  208. if (parent == null)
  209. parent = GetRoot();
  210. // build the file entry
  211. var newEntry = GenericStorageProviderFactory.CreateFileSystemEntry(Session, name, parent);
  212. return newEntry;
  213. }
  214. /// <summary>
  215. /// This method returns the absolut URL (with all authentication decorators) for a specific file
  216. /// system object
  217. /// </summary>
  218. /// <param name="path"></param>
  219. /// <param name="parent"></param>
  220. /// <returns></returns>
  221. public virtual Uri GetFileSystemObjectUrl(string path, ICloudDirectoryEntry parent)
  222. {
  223. var url = Service.GetResourceUrl(Session, parent, path);
  224. return new Uri(url);
  225. }
  226. /// <summary>
  227. /// This method returns the filesystem path (UNIX style) of a specific object
  228. /// </summary>
  229. /// <param name="fsObject"></param>
  230. /// <returns></returns>
  231. public virtual string GetFileSystemObjectPath(ICloudFileSystemEntry fsObject)
  232. {
  233. if (fsObject is ICloudDirectoryEntry)
  234. return GenericHelper.GetResourcePath(fsObject);
  235. return GenericHelper.GetResourcePath(fsObject.Parent) + "/" + fsObject.Id;
  236. }
  237. /// <summary>
  238. /// This method stores the given security token into a tokendictionary
  239. /// </summary>
  240. /// <param name="tokendata"></param>
  241. /// <param name="token"></param>
  242. public void StoreToken(Dictionary<string, string> tokendata, ICloudStorageAccessToken token)
  243. {
  244. Service.StoreToken(Session, tokendata, token);
  245. }
  246. /// <summary>
  247. /// This method loads the given token from the token dictionary
  248. /// </summary>
  249. /// <param name="tokendata"></param>
  250. /// <returns></returns>
  251. public ICloudStorageAccessToken LoadToken(Dictionary<string, string> tokendata)
  252. {
  253. return Service.LoadToken(tokendata);
  254. }
  255. /// <summary>
  256. /// This property returns the current accesstoken
  257. /// </summary>
  258. public ICloudStorageAccessToken CurrentAccessToken
  259. {
  260. get { return Session == null ? null : Session.SessionToken; }
  261. }
  262. #endregion
  263. }
  264. }