/// <summary> /// Create new version for the specified file and return StoredFileVersion object. /// If file isn't version controlled - default version will be returned /// </summary> /// <param name="file">file object</param> /// <returns>StoredFileVersion object (not saved to DB if file is version controlled)</returns> public async Task <StoredFileVersion> GetNewOrDefaultVersionAsync([NotNull] StoredFile file) { if (file == null) { throw new Exception("file should not be null"); } var lastVersion = await GetLastVersionAsync(file); if (file.IsVersionControlled || lastVersion == null) { var newVersion = new StoredFileVersion { File = file, VersionNo = (lastVersion?.VersionNo ?? 0) + 1, FileName = file.FileName, FileType = file.FileType }; await VersionRepository.InsertAsync(newVersion); return(newVersion); } else { return(lastVersion); } }
/// <summary> /// Get url for downloading of the StoredFileVersion /// </summary> /// <param name="storedFileVersion"></param> /// <returns></returns> public static string GetFileVersionUrl(this StoredFileVersion storedFileVersion) { var httpContextAccessor = StaticContext.IocManager.Resolve <IHttpContextAccessor>(); var linkGenerator = StaticContext.IocManager.Resolve <LinkGenerator>(); return(linkGenerator.GetUriByAction(httpContextAccessor.HttpContext, "Download", "StoredFile", new { Id = storedFileVersion.File.Id, versionNo = storedFileVersion.VersionNo })); }
/// inheritedDoc protected override void DeleteFromStorage(StoredFileVersion version) { var path = PhysicalFilePath(version); if (File.Exists(path)) { File.Delete(path); } }
/// <summary> /// Copy file to a new owner /// </summary> public virtual async Task <StoredFile> CopyToOwnerAsync <TId>(StoredFile file, IEntity <TId> newOwner, bool throwCopyException = true) { // todo: move to the base class and reuse in the AzureFileService var newFile = new StoredFile(EntityConfigurationStore) { Description = file.Description, FileName = file.FileName, FileType = file.FileType, Folder = file.Folder, IsVersionControlled = file.IsVersionControlled, Category = file.Category }; newFile.SetOwner(newOwner); await FileRepository.InsertAsync(newFile); // copy versions var versions = await GetFileVersionsAsync(file); foreach (var version in versions) { var newVersion = new StoredFileVersion { File = newFile, VersionNo = version.VersionNo, FileName = version.FileName, FileType = version.FileType, FileSize = version.FileSize }; await VersionRepository.InsertAsync(newVersion); // copy file on the disk try { try { CopyFile(version, newVersion); } catch (FileNotFoundException) { // If we copy missing file, don't fail. } } catch (Exception) { if (throwCopyException) { throw; } } } return(newFile); }
/// inheritedDoc public override async Task UpdateVersionContentAsync(StoredFileVersion version, Stream stream) { if (stream == null) { throw new Exception($"{nameof(stream)} must not be null"); } var blob = GetBlobClient(GetAzureFileName(version)); // update properties version.FileSize = stream.Length; await VersionRepository.UpdateAsync(version); await blob.UploadAsync(stream, overwrite : true); }
public override Stream GetStream(StoredFileVersion fileVersion) { var blob = GetBlobClient(GetAzureFileName(fileVersion)); var stream = new MemoryStream(); // note: Azure throws an exception if file is empty var props = blob.GetProperties(); if (props.Value.ContentLength > 0) { var downloadResult = blob.DownloadTo(stream); stream.Seek(0, SeekOrigin.Begin); } return(stream); }
/// <summary> /// Returns physical path of the specified version of the specified <paramref name="file"/> /// </summary> public string PhysicalFilePath(StoredFileVersion fileVersion) { if (fileVersion == null) { return(null); } var folder = fileVersion.File.Folder; var path = _pathHelper.Combine( !string.IsNullOrWhiteSpace(folder) && (Path.IsPathRooted(folder) || folder.StartsWith("~")) ? "" : _sheshaSettings.UploadFolder, folder ?? "", fileVersion.Id + fileVersion.FileType); return(path); }
/// inheritedDoc protected override void CopyFile(StoredFileVersion source, StoredFileVersion destination) { var sourceBlob = GetBlobClient(GetAzureFileName(source)); if (!sourceBlob.Exists()) { return; } var destinationBlob = GetBlobClient(GetAzureFileName(destination)); if (destinationBlob.Exists()) { return; } destinationBlob.StartCopyFromUri(sourceBlob.Uri); }
public override Stream GetStream(StoredFileVersion fileVersion) { if (fileVersion == null) { return(null); } using (var fileStream = new FileStream(PhysicalFilePath(fileVersion), FileMode.Open, FileAccess.Read)) { // copy to MemoryStream to prevent sharing violations // todo: add a setting and test with FileStream in the real application (check RAM usage) var result = new MemoryStream(); fileStream.CopyTo(result); result.Seek(0, SeekOrigin.Begin); return(result); } }
/// inheritedDoc public override async Task UpdateVersionContentAsync(StoredFileVersion version, Stream stream) { if (stream == null) { throw new Exception($"{nameof(stream)} must not be null"); } var filePath = PhysicalFilePath(version); // delete old file if (File.Exists(filePath)) { File.Delete(filePath); } // create directory if missing var dir = Path.GetDirectoryName(filePath); if (string.IsNullOrWhiteSpace(dir)) { throw new Exception($"File path is not specified. Possible reason: ({nameof(ISheshaSettings.UploadFolder)} is not specified)"); } if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } // update properties version.FileSize = stream.Length; await VersionRepository.UpdateAsync(version); await using (var fs = new FileStream(filePath, FileMode.Create)) { await stream.CopyToAsync(fs); } }
public abstract Stream GetStream(StoredFileVersion fileVersion);
public abstract Task <Stream> GetStreamAsync(StoredFileVersion fileVersion);
/// <summary> /// Delete file version from DB and storage /// </summary> public void Delete(StoredFileVersion version) { VersionRepository.Delete(version); DeleteFromStorage(version); }
/// <summary> /// Delete file version from DB and storage /// </summary> public async Task DeleteAsync(StoredFileVersion version) { await VersionRepository.DeleteAsync(version); await DeleteFromStorageAsync(version); }
/// Delete physical file from the storage (disk/blob storage etc.) protected abstract void DeleteFromStorage(StoredFileVersion version);
/// Delete physical file from the storage (disk/blob storage etc.) protected abstract Task DeleteFromStorageAsync(StoredFileVersion version);
protected abstract void CopyFile(StoredFileVersion source, StoredFileVersion destination);
/// <summary> /// Update content of the specified file version /// </summary> /// <param name="version">File version</param> /// <param name="stream">Stream with file data</param> public abstract Task UpdateVersionContentAsync(StoredFileVersion version, Stream stream);
/// inheritedDoc protected override async Task DeleteFromStorageAsync(StoredFileVersion version) { var blob = GetBlobClient(GetAzureFileName(version)); await blob.DeleteAsync(); }
public async Task MarkDownloadedAsync(StoredFileVersion fileVersion) { // todo: implement }
/// inheritedDoc protected override void DeleteFromStorage(StoredFileVersion version) { var blob = GetBlobClient(GetAzureFileName(version)); blob.Delete(); }
private string GetAzureFileName(StoredFileVersion version) { return(version.Id + version.FileType); }
/// inheritedDoc protected override void CopyFile(StoredFileVersion source, StoredFileVersion destination) { File.Copy(PhysicalFilePath(source), PhysicalFilePath(destination)); }
/// inheritedDoc protected override Task DeleteFromStorageAsync(StoredFileVersion version) { DeleteFromStorage(version); return(Task.CompletedTask); }