Exemplo n.º 1
0
 private async Task WriteToFolder(FileDocument document, Stream documentStream, string ownerId, string folderId)
 {
     var libraryLocation = (await _folderManager.GetDocumentLibraryAsync(ownerId)).Location;
     await _ulService.SetFileStreamAsync(
         document,
         documentStream,
         libraryLocation);
 }
Exemplo n.º 2
0
        private async void EnsurePhysicalFile(FileDocument doc, string libraryLocation)
        {
            var stream = await _uploadService.GetFileStreamAsync(doc, libraryLocation);

            if (stream == null)
            {
                using (stream = CreateTemporarySeedData(doc))
                {
                    if (stream.Length > 0)
                    {
                        await _uploadService.SetFileStreamAsync(doc, stream, libraryLocation);
                    }
                }
            }
            else
            {
                stream.Dispose();
            }
        }
Exemplo n.º 3
0
        //public async Task<Folder> AddFolderAsync(string name, string ownerId, string parentId = null)
        //{
        //    Ensure.NotNullOrEmpty(name, $"{name} cannot be null or empty.");
        //    Ensure.NotNullOrEmpty(ownerId, $"{ownerId} cannot be null or empty.");

        //    var result = new Folder()
        //    {
        //        DocumentType = typeof(FileDocument).FullName,
        //        FolderType = typeof(Folder).FullName,
        //        OwnerLevel = OwnerLevel.User,
        //        OwnerId = ownerId,
        //        CreatedBy = ownerId,
        //        FolderFlags = FolderFlag.Shared,
        //        Id = KeyGenerator.CreateShortGuid(),
        //        ParentFolder = string.IsNullOrEmpty(parentId) ? null : (await GetFolderContentsAsync(parentId)),
        //        ParentId = parentId,
        //        Title = name
        //    };

        //    result = (await _folderManager.CreateFolderAsync(result)).ToFolder();

        //    return result;
        //}

        //public async Task RemoveFolderAsync(string folderId)
        //{
        //    Ensure.NotNullOrEmpty(folderId, $"{folderId} cannot be null or empty.");

        //    var folder = await _folderManager.GetFolderAsync(folderId);
        //    var parent = string.IsNullOrEmpty(folder.ParentId) ? null : await _folderManager.GetFolderAsync(folder.ParentId);

        //    if (await IsTrashFolderAsync(parent)) //(parent.IsTrashFolder())
        //    {
        //        await _folderManager.RemoveFolderAsync(folder);
        //    }
        //    else
        //    {
        //        // Recycle
        //        await _folderManager.MoveFolderAsync(folder, await GetTrashFolderAsync(folder.OwnerId));
        //    }
        //}

        //public async Task RenameFolderAsync(string ownerId, string folderId, string newName)
        //{
        //    // NOT WORKING YET (Requires Federation to work) Ensure.NotNullOrEmpty(ownerId, $"{ownerId} cannot be null or empty.");
        //    Ensure.NotNullOrEmpty(folderId, $"{folderId} cannot be null or empty.");
        //    Ensure.NotNullOrEmpty(newName, $"{newName} cannot be null or empty.");

        //    var folder = await _folderManager.GetFolderAsync(folderId);
        //    folder.Title = newName;
        //    await _folderManager.UpdateFolderAsync(folder);
        //}

        //public async Task MoveFolderAsync(string ownerId, string folderId, string destinationId)
        //{
        //    // NOT WORKING YET (Requires Federation to work) Ensure.NotNullOrEmpty(ownerId, $"{ownerId} cannot be null or empty.");
        //    Ensure.NotNullOrEmpty(folderId, $"{folderId} cannot be null or empty.");
        //    Ensure.NotNullOrEmpty(destinationId, $"{destinationId} cannot be null or empty.");

        //    var folder = await _folderManager.GetFolderAsync(folderId);
        //    var destination = await _folderManager.GetFolderAsync(destinationId);

        //    await _folderManager.MoveFolderAsync(folder, destination);
        //}

        //public async Task CopyFolderAsync(string ownerId, string folderId, string destinationId, string newName)
        //{
        //    // NOT WORKING YET (Requires Federation to work) Ensure.NotNullOrEmpty(ownerId, $"{ownerId} cannot be null or empty.");
        //    Ensure.NotNullOrEmpty(folderId, $"{folderId} cannot be null or empty.");
        //    Ensure.NotNullOrEmpty(destinationId, $"{destinationId} cannot be null or empty.");

        //    var folder = await _folderManager.GetFolderAsync(folderId);
        //    var destination = await _folderManager.GetFolderAsync(destinationId);

        //    await CopyFolderAsync(folder, destination, newName);
        //}

        //private async Task CopyFolderAsync(IFolder folder, IFolder destination, string newName)
        //{
        //    newName = (newName ?? string.Empty).Trim();

        //    folder.Id = KeyGenerator.CreateShortGuid();
        //    folder.Title = newName ?? folder.Title;
        //    folder.ParentId = destination.Id;

        //    await _folderManager.CreateFolderAsync(folder);

        //    var contents = await GetFolderContentsAsync(folder.Id);
        //    // Copy the files in this folder
        //    foreach (var document in await GetDocumentsInFolder(folder.Id))
        //    {
        //        await CopyDocumentAsync(document.DocumentId, folder.ParentId, folder.Id, null);
        //    }

        //    // Copy the folders in this folder
        //    foreach (var subFolder in contents.ChildFolders)
        //    {
        //        await CopyFolderAsync(subFolder, folder, null);
        //    }
        //}

        //public async Task AddFolderTag(string id, int tagId)
        //{
        //    Ensure.Argument.NotNullOrEmpty(id, "id");
        //    Ensure.Argument.NotNullOrEmpty(tagId.ToString(), "tagId");

        //    var tag = await _tagManager.GetById(tagId);
        //    var folder = await _folderManager.GetFolderAsync(id);

        //    await _tagManager.AddTag(folder, tag);
        //}

        //public async Task RemoveFolderTag(string id, int tagId)
        //{
        //    Ensure.Argument.NotNullOrEmpty(id, "id");
        //    Ensure.Argument.NotNullOrEmpty(tagId.ToString(), "tagId");

        //    var tag = await _tagManager.GetById(tagId);
        //    var folder = await _folderManager.GetFolderAsync(id);

        //    await _tagManager.RemoveTag(folder, tag);
        //}
        //#endregion // Folders
        //#region Documents

        //public async Task UpdateDocument(FileDocument document, string userId)
        //{
        //    Ensure.Argument.NotNull(document);

        //    await _documentService.UpdateAsync(document);

        //    await _log.LogEventWriteAsync(document, userId);
        //}

        //// NOTE: isLogged allows internal usage of GetDocument without showing them as user-level log events. Really only want to log
        //// explicit reads done by the user (i.e. by public controller methods whose purpose is to actually return document information)
        //public async Task<FileDocument> GetDocumentAsync(string documentId, string userId, bool isLogged = true)
        //{
        //    if (String.IsNullOrEmpty(documentId))
        //        throw new ArgumentNullException($"{nameof(documentId)} cannot be null.");

        //    var result = await _documentService.GetAsync(documentId);

        //    if (isLogged && result != null)
        //    {
        //        await _log.LogEventReadAsync(result, userId);
        //    }

        //    return result;
        //}

        //public async Task<List<FileDocument>> GetDocumentsAsync(string ownerId)
        //{
        //    if (String.IsNullOrEmpty(ownerId))
        //        throw new ArgumentNullException("ownerId cannot be null.");

        //    var folders = await _folderManager.GetFoldersAsync(ownerId);
        //    var results = folders.SelectMany(x => _folderManager.GetDocumentsAsync(x).GetAwaiter().GetResult())
        //        .ToList();

        //    await Task.WhenAll(results.Select(doc => _log.LogEventReadAsync(doc, ownerId)));

        //    return results;
        //}

        //public async Task<IEnumerable<FileDocument>> GetDocumentsInFolder(string folderId)
        //{
        //    if (String.IsNullOrEmpty(folderId))
        //        throw new ArgumentNullException("ownerId folderId be null.");

        //    var folder = await _folderManager.GetFolderAsync(folderId);
        //    var results = await _folderManager.GetDocumentsAsync(folder);

        //    await Task.WhenAll(results.Select(doc => _log.LogEventReadAsync(doc, folderId)));

        //    return results;
        //}

        //public async Task<FileDocument> AddDocumentAsync(string fileFullPath, string fileType, string folderId, IFormFile file, string documentId = null)
        //{
        //    Ensure.Argument.Is(file.Length > 0, "Document must be supplied.");
        //    Ensure.Argument.NotNullOrEmpty(folderId);
        //    Ensure.NotNullOrEmpty(fileFullPath);

        //    var fileName = Path.GetFileName(fileFullPath);

        //    var document = new FileDocument()
        //    {
        //        DocumentId = string.IsNullOrEmpty(documentId) ? KeyGen.NewGuid() : documentId,
        //        FileName = fileName,
        //    };
        //    return await AddDocumentAsync(file, folderId, document);
        //}

        //public async Task<FileDocument> AddDocumentAsync(IFormFile file, string folderId, FileDocument document)
        //{
        //    var folder = await _folderManager.GetFolderAsync(folderId);
        //    System.Diagnostics.Debug.Assert(folder != null);    // Shouldn't ever trip due to Ensure, but First() throws an ugly excception

        //    document.ContentLength = file.Length;
        //    document.FileType = document.GetFileType();

        //    using (var stream = file.OpenReadStream())
        //    {
        //        await _uploadService.SetFileStreamAsync(document, stream);
        //    }

        //    await _documentService.CreateAsync(document);
        //    await _folderManager.AddDocumentAsync(document, folder);

        //    await _log.LogEventCreateAsync(document, folder.OwnerId);

        //    return document;
        //}

        //public async Task RemoveDocumentAsync(string id)
        //{
        //    Ensure.Argument.NotNullOrEmpty(id);

        //    await _documentService.DeleteAsync(id);
        //}

        //public async Task MoveDocumentAsync(string documentId, string sourceFolderId, string destinationFolderId)
        //{
        //    Ensure.Argument.NotNullOrEmpty(destinationFolderId);
        //    Ensure.Argument.NotNullOrEmpty(documentId);

        //    var document = await _documentService.GetAsync(documentId);
        //    var source = await _folderManager.GetFolderAsync(sourceFolderId);
        //    var destination = await _folderManager.GetFolderAsync(destinationFolderId);

        //    await _folderManager.MoveDocumentAsync(document, source, destination);

        //    await _log.LogEventWriteAsync(document, destination.OwnerId);
        //}

        //public async Task CopyDocumentAsync(string documentId, string sourceFolderId, string destinationFolderId, string newName)
        //{
        //    Ensure.Argument.NotNullOrEmpty(destinationFolderId);
        //    Ensure.Argument.NotNullOrEmpty(documentId);

        //    var documentToMove = await _documentService.GetAsync(documentId);
        //    var originFolder = await _folderManager.GetFolderAsync(sourceFolderId);
        //    var destinationFolder = await _folderManager.GetFolderAsync(destinationFolderId);

        //    var originalId = documentToMove.DocumentId;
        //    var originalOwnerId = originFolder.OwnerId;

        //    newName = newName?.Trim();
        //    if (!string.IsNullOrEmpty(newName) && documentToMove.FileName != newName)
        //    {
        //        // Rename
        //        documentToMove.FileName = newName;
        //    }

        //    // Copy the document object
        //    await _folderManager.CopyDocumentAsync(documentToMove, destinationFolder);

        //    // Log it
        //    await _log.LogEventCreateAsync(documentToMove, destinationFolder.OwnerId);
        //}

        //public async Task RenameDocumentAsync(string documentId, string newName, string userId)
        //{
        //    Ensure.Argument.NotNullOrEmpty(documentId);
        //    Ensure.Argument.NotNullOrEmpty(newName);

        //    var document = await _documentService.GetAsync(documentId);
        //    document.FileName = newName;
        //    await _documentService.UpdateAsync(document);

        //    await _log.LogEventWriteAsync(document, userId);
        //}

        public async Task UploadDocumentAsync(string id, string userId, Stream file)
        {
            Ensure.NotNullOrEmpty(id, $"{nameof(id)} cannot be null.");
            Ensure.NotNull(file, $"{nameof(file)} cannot be null.");

            var document = await GetFileDocument(id);

            var physicalLocation = await GetLibraryPhysicalLocation(id);

            //Save original file.
            await _uploadService.SetFileStreamAsync(document, file, physicalLocation);

            //apply post processing tasks
            foreach (var processor in _imageProcessors)
            {
                await processor.Invoke(physicalLocation, document);
            }

            await _log.LogEventWriteAsync(document, userId);
        }