void Copy(FileItemPathInfo sourcePathInfo, FileItemPathInfo destinationPathInfo, bool deleteSource = false) { string sourceKey = GetFileItemPath(sourcePathInfo); string destinationKey = GetFileItemPath(destinationPathInfo) + "/" + sourcePathInfo.GetFileItemName(); Copy(sourceKey, destinationKey, deleteSource); }
public IList <IClientFileSystemItem> GetDirectoryContents(FileItemPathInfo pathInfo) { var fileItems = GetDirectoryContents(pathInfo.GetFileItemKey <int>()); var hasSubDirectoriesInfo = GetHasSubDirectoriesInfo(fileItems); var clientItemList = new List <IClientFileSystemItem>(); foreach (var item in fileItems) { var clientItem = new ClientFileSystemItem { Key = item.Id, Name = item.Name, IsDirectory = item.IsDirectory, DateModified = item.Modified }; if (item.IsDirectory) { clientItem.HasSubDirectories = hasSubDirectoriesInfo.ContainsKey(item.Id) && hasSubDirectoriesInfo[item.Id]; } clientItem.CustomFields["modifiedBy"] = item.ModifiedBy.FullName; clientItem.CustomFields["created"] = item.Created; clientItemList.Add(clientItem); } return(clientItemList); }
public void Copy(FileItemPathInfo sourcePathInfo, FileItemPathInfo destinationPathInfo) { if (!IsFileItemExists(sourcePathInfo)) { FileManagementExceptionExecutor.ThrowDirectoryExists(sourcePathInfo.GetPath()); } if (!IsFileItemExists(destinationPathInfo)) { FileManagementExceptionExecutor.ThrowDirectoryExists(sourcePathInfo.GetPath()); } if (!AllowCopyOrMove(sourcePathInfo, destinationPathInfo)) { FileManagementExceptionExecutor.ThrowNoAccess(); } var sourceFileItem = GetFileItem(sourcePathInfo); var copyFileItem = CreateCopy(sourceFileItem); copyFileItem.ParentId = destinationPathInfo.GetFileItemKey <int>(); copyFileItem.Name = GenerateCopiedFileItemName(destinationPathInfo.GetFileItemKey <int>(), copyFileItem.Name, copyFileItem.IsDirectory); FileManagementDbContext.FileItems.Add(copyFileItem); if (copyFileItem.IsDirectory) { CopyDirectoryContentRecursive(sourceFileItem, copyFileItem); } FileManagementDbContext.SaveChanges(); }
public IList <IClientFileSystemItem> GetDirectoryContents(FileItemPathInfo pathInfo) { var result = new List <IClientFileSystemItem>(); BlobContinuationToken continuationToken = null; string dirKey = GetFileItemPath(pathInfo); if (!string.IsNullOrEmpty(dirKey)) { dirKey = dirKey + "/"; } CloudBlobDirectory dir = Container.GetDirectoryReference(dirKey); do { BlobResultSegment segmentResult = dir.ListBlobsSegmented(continuationToken); continuationToken = segmentResult.ContinuationToken; foreach (IListBlobItem blob in segmentResult.Results) { var item = new ClientAzureFileSystemItem(); string name = GetFileItemName(blob); if (name == EmptyDirectoryDummyBlobName) { continue; } if (blob is CloudBlob) { var blockBlob = (CloudBlob)blob; item.Name = name; item.DateModified = blockBlob.Properties.LastModified.GetValueOrDefault().DateTime; item.Size = blockBlob.Properties.Length; } else if (blob is CloudBlobDirectory) { var subDir = (CloudBlobDirectory)blob; item.Name = name.Substring(0, name.Length - 1); item.IsDirectory = true; item.HasSubDirectories = GetHasDirectories(subDir); item.DateModified = DateTime.UtcNow; } else { throw new Exception("Unsupported blob type"); } result.Add(item); } } while(continuationToken != null); return(result.OrderByDescending(item => item.IsDirectory) .ThenBy(item => item.Name) .ToList()); }
public void CreateDirectory(FileItemPathInfo pathInfo, string name) { string path = GetFileItemPath(pathInfo); string blobKey = $"{name}/{EmptyDirectoryDummyBlobName}"; if (!string.IsNullOrEmpty(path)) { blobKey = $"{path}/{blobKey}"; } CloudBlockBlob dirBlob = Container.GetBlockBlobReference(blobKey); dirBlob.UploadText(""); }
public void Rename(FileItemPathInfo pathInfo, string newName) { if (!IsFileItemExists(pathInfo)) { FileManagementExceptionExecutor.ThrowDirectoryExists(pathInfo.GetPath()); } var fileItem = GetFileItem(pathInfo); fileItem.Name = newName; fileItem.ModifiedById = GuestPersonId; fileItem.Modified = DateTime.Now; FileManagementDbContext.SaveChanges(); }
public void Remove(FileItemPathInfo pathInfo) { string key = GetFileItemPath(pathInfo); CloudBlob blob = Container.GetBlobReference(key); bool isFile = blob.Exists(); if (isFile) { RemoveFile(blob); } else { RemoveDirectory(key + "/"); } }
public void Rename(FileItemPathInfo pathInfo, string newName) { string key = GetFileItemPath(pathInfo); int index = key.LastIndexOf('/'); string newKey; if (index >= 0) { string parentKey = key.Substring(0, index + 1); newKey = parentKey + newName; } else { newKey = newName; } Copy(key, newKey, true); }
public void Remove(FileItemPathInfo pathInfo) { if (!IsFileItemExists(pathInfo)) { FileManagementExceptionExecutor.ThrowDirectoryExists(pathInfo.GetPath()); } var fileItem = GetFileItem(pathInfo); FileManagementDbContext.FileItems.Remove(fileItem); if (fileItem.IsDirectory) { RemoveDirectoryContentRecursive(fileItem.Id); } FileManagementDbContext.SaveChanges(); }
static bool AllowCopyOrMove(FileItemPathInfo pathInfo, FileItemPathInfo destinationPathInfo) { var sourcePathParts = pathInfo.GetPathParts <int>(); var destinationPathParts = destinationPathInfo.GetPathParts <int>(); if (destinationPathParts.Length < sourcePathParts.Length) { return(true); } var isValid = false; for (var i = 0; i < destinationPathParts.Length && !isValid; i++) { isValid = destinationPathParts[i].Key != sourcePathParts[i].Key; } return(isValid); }
public void CreateDirectory(FileItemPathInfo pathInfo, string name) { if (!IsFileItemExists(pathInfo)) { FileManagementExceptionExecutor.ThrowDirectoryExists(pathInfo.GetPath()); } var directory = new FileItem { Name = name, Modified = DateTime.Now, Created = DateTime.Now, IsDirectory = true, ParentId = pathInfo.GetFileItemKey <int>(), ModifiedById = GuestPersonId }; FileManagementDbContext.FileItems.Add(directory); FileManagementDbContext.SaveChanges(); }
public Stream GetFileContent(FileItemPathInfo pathInfo) { if (!Directory.Exists(TempDirectoryPath)) { Directory.CreateDirectory(TempDirectoryPath); } CleanUpDownloadedFiles(); string tempFileName = string.Format("{0}{1}.tmp", TempFilePrefix, Guid.NewGuid().ToString("N")); string tempFilePath = Path.Combine(TempDirectoryPath, tempFileName); string key = GetFileItemPath(pathInfo); CloudBlockBlob blob = Container.GetBlockBlobReference(key); blob.DownloadToFile(tempFilePath, FileMode.Create); return(File.Open(tempFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)); }
public void Move(FileItemPathInfo pathInfo, FileItemPathInfo destinationPathInfo) { if (!IsFileItemExists(pathInfo)) { FileManagementExceptionExecutor.ThrowDirectoryExists(pathInfo.GetPath()); } if (!IsFileItemExists(destinationPathInfo)) { FileManagementExceptionExecutor.ThrowDirectoryExists(pathInfo.GetPath()); } if (!AllowCopyOrMove(pathInfo, destinationPathInfo)) { FileManagementExceptionExecutor.ThrowNoAccess(); } var fileItem = GetFileItem(pathInfo); fileItem.ParentId = destinationPathInfo.GetFileItemKey <int>(); fileItem.Modified = DateTime.Now; fileItem.ModifiedById = GuestPersonId; FileManagementDbContext.SaveChanges(); }
bool IsFileItemExists(FileItemPathInfo pathInfo) { var clientPathParts = pathInfo.GetPathParts <int>(); var pathKeys = clientPathParts.Select(p => p.Key).ToArray(); var foundEntries = FileManagementDbContext.FileItems .Where(item => pathKeys.Contains(item.Id)) .Select(item => new { item.Id, item.ParentId, item.Name, item.IsDirectory }); var isDirectoryExists = true; for (var i = 0; i < clientPathParts.Length && isDirectoryExists; i++) { var entry = foundEntries.FirstOrDefault(e => e.Id == clientPathParts[i].Key); isDirectoryExists = entry != null && entry.Name == clientPathParts[i].Name && (i == 0 && entry.ParentId == 0 || entry.ParentId == clientPathParts[i - 1].Key); if (isDirectoryExists && i < clientPathParts.Length - 1) { isDirectoryExists = entry.IsDirectory; } } return(isDirectoryExists); }
string GetFileItemPath(FileItemPathInfo pathInfo) { return(pathInfo.GetPath().Replace('\\', '/')); }
public void Copy(FileItemPathInfo sourcePathInfo, FileItemPathInfo destinationPathInfo) { Copy(sourcePathInfo, destinationPathInfo, false); }
public void Move(FileItemPathInfo pathInfo, FileItemPathInfo destinationPathInfo) { Copy(pathInfo, destinationPathInfo, true); }
public Stream GetFileContent(FileItemPathInfo pathInfo) { throw new System.NotImplementedException(); }
FileItem GetFileItem(FileItemPathInfo pathInfo) { return(FileManagementDbContext.FileItems.FirstOrDefault(i => i.Id == pathInfo.GetFileItemKey <int>())); }