private WopiFile CreateOwnerRecords(string fileName, string userId, CloudBlockBlob blockBlob, int version) { var wopiFile = new WopiFile() { FileId = blockBlob.Name, FileName = fileName, FileExtension = Path.GetExtension(fileName), OwnerId = userId.ToLower(), Size = blockBlob.Properties.Length, Container = blockBlob.Container.Name, Version = version, LastModifiedTime = blockBlob.Properties.LastModified.Value, LastModifiedUser = userId.ToLower(), FilePermissions = new HashSet <WopiFilePermission>() }; wopiFile.FilePermissions.Add(new WopiFilePermission() { File = wopiFile, ReadOnly = false, UserCanWrite = true, UserCanRename = true, UserCanNotWriteRelative = false, UserCanPresent = true, UserCanAttend = true, UserId = userId }); return(wopiFile); }
private async Task <WopiFile> CreateCopyInternal(CloudBlobClient blobClient, WopiContext wopiContext, WopiFile originalFile, string newFileName, string userId) { CloudBlockBlob newBlob = null; WopiFile newWopiFile = null; try { var newFileId = Guid.NewGuid().ToString(); var container = blobClient.GetContainerReference(originalFile.Container); var existingBlob = container.GetBlockBlobReference(originalFile.FileId); newBlob = container.GetBlockBlobReference(newFileId); var copyOperation = await newBlob.StartCopyAsync(existingBlob); await WaitForCopyAsync(newBlob); newWopiFile = CreateOwnerRecords(newFileName, userId, newBlob, 1); } catch (Exception ex) { if (newBlob != null && await newBlob.ExistsAsync()) { await newBlob.DeleteAsync(); } throw; } return(newWopiFile); }
/// <summary> /// Replaces file content with data in provide file stream if user has appropriate rights. Currently only the owner is allowed to UpdateFileContents. /// </summary> /// <param name="fileId"></param> /// <param name="userId"></param> /// <param name="lockValue"></param> /// <param name="stream"></param> /// <returns></returns> public async Task <Tuple <HttpStatusCode, string, string> > UpdateFileContent(string fileId, string userId, string lockValue, Stream stream) { using (var wopiContext = new WopiContext()) { var filePermission = await wopiContext.FilePermissions.Where(fp => fp.FileId == fileId && fp.UserId == userId).Include("File").FirstOrDefaultAsync(); if (filePermission == null) { return(new Tuple <HttpStatusCode, string, string>(HttpStatusCode.NotFound, null, null)); } WopiFile file = filePermission.File; if (!file.IsLocked) { if (file.Size > 0) { return(new Tuple <HttpStatusCode, string, string>(HttpStatusCode.Conflict, string.Empty, null)); } } else { if (!file.IsSameLock(lockValue)) { return(new Tuple <HttpStatusCode, string, string>(HttpStatusCode.Conflict, file.LockValue, null)); } } var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); var blobClient = storageAccount.CreateCloudBlobClient(); var containerName = file.Container; var container = blobClient.GetContainerReference(containerName); await container.CreateIfNotExistsAsync(); var blockBlob = container.GetBlockBlobReference(fileId); await blockBlob.UploadFromStreamAsync(stream); file.Size = blockBlob.Properties.Length; file.LastModifiedTime = blockBlob.Properties.LastModified.Value; file.LastModifiedUser = userId.ToLower(); await wopiContext.SaveChangesAsync(); return(new Tuple <HttpStatusCode, string, string>(HttpStatusCode.OK, null, file.Version.ToString())); } }
/// <summary> /// Creates a copy of the specified file using an generated file name, /// which is returned as the output. /// </summary> /// <param name="fileId">Unique ID of source file</param> /// <param name="userId">UserID of user the initiating copy. This will be the owner of the file copy.</param> /// <returns>Name of the new file or null if the file was not found or the specified user does not have permissions to the file</returns> public async Task <Tuple <HttpStatusCode, WopiFile> > CreateCopySuggested(string fileId, string userId, string suggestedCopyName) { using (var wopiContext = new WopiContext()) { var originalFile = await wopiContext.Files.Where(f => f.FileId == fileId && f.FilePermissions.Any(fp => fp.UserId == userId)).FirstOrDefaultAsync(); if (originalFile == null) { return(new Tuple <HttpStatusCode, WopiFile>(HttpStatusCode.NotFound, null)); } if (suggestedCopyName.StartsWith(".")) { suggestedCopyName = Path.GetFileNameWithoutExtension(originalFile.FileName) + suggestedCopyName; } var newFileName = FileNameUtil.MakeValidFileName(suggestedCopyName); WopiFile existingFile = null; do { existingFile = await wopiContext.Files.Where(f => f.FileName == newFileName).FirstOrDefaultAsync(); if (existingFile != null) { newFileName = Path.GetFileNameWithoutExtension(newFileName) + "-copy" + Path.GetExtension(newFileName); } }while (existingFile != null); var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); var blobClient = storageAccount.CreateCloudBlobClient(); var newFile = await CreateCopyInternal(blobClient, wopiContext, originalFile, newFileName, userId); wopiContext.Files.Add(newFile); await wopiContext.SaveChangesAsync(); return(new Tuple <HttpStatusCode, WopiFile>(HttpStatusCode.OK, newFile)); } }