StorageService.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.IO;
  3. using System.IO.Abstractions;
  4. using System.Security.Principal;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using BO.AppServer.Business.Exceptions;
  8. using BO.AppServer.Data.Entity;
  9. using BO.AppServer.Data.Repository;
  10. using BO.AppServer.Metadata.Configuration;
  11. using BO.AppServer.Metadata.Enums;
  12. using Microsoft.Extensions.Logging;
  13. using Microsoft.Extensions.Options;
  14. using Quadarax.Foundation.Core.Business;
  15. using Quadarax.Foundation.Core.Exceptions;
  16. using Quadarax.Foundation.Core.Value.Extensions;
  17. namespace BO.AppServer.Business.Services
  18. {
  19. public class StorageService : AbstractService
  20. {
  21. #region *** Private fields ***
  22. private IFileSystem _fileSystem;
  23. private IOptions<Configuration> _config;
  24. private ArtifactRepo _repoArtifact;
  25. #endregion
  26. #region *** Constructors ***
  27. public StorageService(ArtifactRepo repoArtifact,
  28. IOptions<Configuration> config,
  29. IFileSystem fileSystem,
  30. IPrincipal currentPrincipal,
  31. ILoggerFactory logger) : base(currentPrincipal, logger)
  32. {
  33. _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
  34. _config = config ?? throw new ArgumentNullException(nameof(config));
  35. _repoArtifact = repoArtifact ?? throw new ArgumentNullException(nameof(repoArtifact));
  36. if (!_fileSystem.Directory.Exists(_config.Value.System.Storage.InputPath.EnsurePathNonBackslash()))
  37. {
  38. _fileSystem.Directory.CreateDirectory(_config.Value.System.Storage.InputPath.EnsurePathNonBackslash());
  39. Log.LogInformation($"Storage Input directory '{_config.Value.System.Storage.InputPath.EnsurePathNonBackslash()}' created.");
  40. }
  41. else
  42. Log.LogInformation($"Storage Input directory '{_config.Value.System.Storage.InputPath.EnsurePathNonBackslash()}' verified.");
  43. if (!_fileSystem.Directory.Exists(_config.Value.System.Storage.OutputPath.EnsurePathNonBackslash()))
  44. {
  45. _fileSystem.Directory.CreateDirectory(_config.Value.System.Storage.OutputPath.EnsurePathNonBackslash());
  46. Log.LogInformation($"Storage Output directory '{_config.Value.System.Storage.OutputPath.EnsurePathNonBackslash()}' created.");
  47. }
  48. else
  49. Log.LogInformation($"Storage Input directory '{_config.Value.System.Storage.InputPath.EnsurePathNonBackslash()}' verified.");
  50. }
  51. #endregion
  52. #region *** Public Operations ***
  53. public async Task<long> PushContentAsync(long artifactId, Stream fileData)
  54. {
  55. var artifact = _repoArtifact.Get(artifactId);
  56. if (artifact == null)
  57. {
  58. var exc = new EntityNotExistsException(typeof(Artifact),nameof(Artifact.Id), artifactId);
  59. Log.LogWarning(exc.Message);
  60. throw exc;
  61. }
  62. var documentId = artifact.Metadocument.Id;
  63. var workspaceId = artifact.Metadocument.Structure.WorkspaceId;
  64. var fileName = GetFileName(artifactId, artifact.Name, artifact.Extension);
  65. var artType = (ArtifactTypeEnum)artifact.Type;
  66. var fullFileName = EnureDirectory(workspaceId, documentId, artType) + _fileSystem.Path.DirectorySeparatorChar + fileName;
  67. long size;
  68. await using (var fs = _fileSystem.File.Create(fullFileName, _config.Value.System.Storage.IoBufferSize, FileOptions.Asynchronous))
  69. {
  70. fileData.Position = 0;
  71. await fileData.CopyToAsync(fs, _config.Value.System.Storage.IoBufferSize);
  72. await fs.FlushAsync();
  73. size = fs.Length;
  74. fs.Close();
  75. }
  76. return size;
  77. }
  78. public async Task<Tuple<Stream, long>> PopContentAsync(long artifactId)
  79. {
  80. var artifact = _repoArtifact.Get(artifactId);
  81. if (artifact == null)
  82. {
  83. var exc = new EntityNotExistsException(typeof(Artifact),nameof(Artifact.Id), artifactId);
  84. Log.LogWarning(exc.Message);
  85. throw exc;
  86. }
  87. var documentId = artifact.Metadocument.Id;
  88. var workspaceId = artifact.Metadocument.Structure.WorkspaceId;
  89. var fileName = GetFileName(artifactId, artifact.Name, artifact.Extension);
  90. var artType = (ArtifactTypeEnum)artifact.Type;
  91. var fullFileName = EnureDirectory(workspaceId, documentId, artType) + _fileSystem.Path.DirectorySeparatorChar + fileName;
  92. if (!_fileSystem.File.Exists(fullFileName))
  93. {
  94. var message = $"Content for artifact '{fullFileName}' [{artifactId}] not found!";
  95. Log.LogWarning(message);
  96. throw new CodeException(1300, message);
  97. }
  98. var buffer = new byte[_config.Value.System.Storage.IoBufferSize];
  99. //var outputStream = new MemoryStream(buffer);
  100. var outputStream = new MemoryStream();
  101. await using (var fs = _fileSystem.File.OpenRead(fullFileName))
  102. {
  103. //await fs.CopyToAsync(outputStream, _config.Value.System.Storage.IoBufferSize);
  104. await fs.CopyToAsync(outputStream);
  105. await outputStream.FlushAsync();
  106. fs.Close();
  107. }
  108. outputStream.Seek(0, SeekOrigin.Begin);
  109. return new Tuple<Stream, long>(outputStream, outputStream.Length);
  110. }
  111. public async Task<bool> DeleteContent(long artifactId)
  112. {
  113. var artifact = _repoArtifact.Get(artifactId);
  114. if (artifact == null)
  115. {
  116. var exc = new EntityNotExistsException(typeof(Artifact),nameof(Artifact.Id), artifactId);
  117. Log.LogWarning(exc.Message);
  118. throw exc;
  119. }
  120. var documentId = artifact.Metadocument.Id;
  121. var workspaceId = artifact.Metadocument.Structure.WorkspaceId;
  122. var fileName = GetFileName(artifactId, artifact.Name, artifact.Extension);
  123. var artType = (ArtifactTypeEnum)artifact.Type;
  124. var fullFileName = EnureDirectory(workspaceId, documentId, artType) + _fileSystem.Path.DirectorySeparatorChar + fileName;
  125. if (!_fileSystem.File.Exists(fullFileName))
  126. {
  127. var message = $"Content for artifact '{fullFileName}' [{artifactId}] not found for delete!";
  128. Log.LogInformation(message);
  129. return false;
  130. }
  131. _fileSystem.File.Delete(fullFileName);
  132. return true;
  133. }
  134. #endregion
  135. #region *** Private Operations ***
  136. private string EnureDirectory(long workspaceId, long documentId, ArtifactTypeEnum type)
  137. {
  138. var dirName = GetDirectoryName(workspaceId, documentId, type);
  139. if (!_fileSystem.Directory.Exists(dirName))
  140. {
  141. _fileSystem.Directory.CreateDirectory(dirName);
  142. Log.LogInformation($"Storage document directory '{dirName}' created.");
  143. }
  144. return dirName;
  145. }
  146. private string GetDirectoryName(long workspaceId, long documentId, ArtifactTypeEnum type)
  147. {
  148. var sb = new StringBuilder();
  149. sb.Append(type == ArtifactTypeEnum.Input ? _config.Value.System.Storage.InputPath.EnsurePathNonBackslash() : _config.Value.System.Storage.OutputPath.EnsurePathNonBackslash()).Append(_fileSystem.Path.DirectorySeparatorChar);
  150. sb.Append(workspaceId).Append(_fileSystem.Path.DirectorySeparatorChar);
  151. sb.Append(documentId);
  152. return sb.ToString();
  153. }
  154. private string GetFileName(long artifactId, string fileNameWoExtension, string extension)
  155. {
  156. var sb = new StringBuilder();
  157. sb.Append(fileNameWoExtension).Append(".");
  158. sb.Append(artifactId).Append(".");
  159. sb.Append(extension);
  160. return sb.ToString();
  161. }
  162. #endregion
  163. }
  164. }