예제 #1
0
        public static bool ValidateFolderForUploading(this ModelState modelState, Node folder, double size)
        {
            if(folder.Owner.AvailableMemory < size)
            {
                var additionalMemory = ((size - folder.Owner.AvailableMemory) / 1024 / 1024).ToString("0.##");
                modelState.ErrorMessage = string.Format("You don't have enough memory space. You need additional {0} Mb", additionalMemory);
            }

            return modelState.IsValid;
        }
예제 #2
0
 public NodeViewModel(Node node)
 {
     this.Id = node.Id;
     this.Name = node.Name;
     this.Type = node.Type.ToString();
     this.Size = node.Size;
     this.RootId = node.RootId;
     this.ThumbnailPath = node.ThumbnailPath;
     this.IsFolder = node.Type == NodeType.Folder;
 }
예제 #3
0
        public static bool ValidateNode(this ModelState modelState, Node node, ICurrentUser currentUser)
        {
            if (node == null)
            {
                modelState.ErrorMessage = "The item is not found";

                return modelState.IsValid;
            }

            if (ValidationHelper.IsUserHasAccess(node, currentUser) == false)
            {
                modelState.ErrorMessage = "Access denied";

                return modelState.IsValid;
            }

            return modelState.IsValid;
        }
예제 #4
0
        public static string GetTypeThumbnailPath(Node node)
        {
            string path;

            if(node.Type == NodeType.Image)
            {
                path = "/api/files/" + node.Id;
            }
            else if(node.Type == NodeType.Folder)
            {
                path = "/Content/images/folder.png";
            }
            else
            {
                path = "/Content/images/" + node.Extension + ".jpg";
            }

            return path;
        }
예제 #5
0
        public async Task AddNodeAsync(Node node)
        {
            var rootFolder = await _nodeRepository.FindAsync(node.RootId);

            if (!this.State.ValidateNode(rootFolder, _currentUser))
            {
                return;
            }

            node.Created = DateTime.Now;
            node.OwnerId = _currentUser.Id;

            if (node.Type != NodeType.Folder)
            {
                if (!this.State.ValidateFolderForUploading(rootFolder, node.Size))
                {
                    return;
                }

                if (node.ContentType == string.Empty)
                {
                    node.ContentType = MimeMapping.GetMimeMapping(node.Name);
                }

                node.Type = FileUploadHelper.GetFileTypeByContentType(node.ContentType);
                node.Extension = Path.GetExtension(node.Name).ToLower();
                rootFolder.Owner.AvailableMemory -= node.Size;
            }

            rootFolder.Siblings.Add(node);
            await _unitOfWork.SaveChangesAsync();

            node.ThumbnailPath = FileUploadHelper.GetTypeThumbnailPath(node);
            await _unitOfWork.SaveChangesAsync();

            return;
        }
예제 #6
0
 private static bool IsUserHasAccess(Node node, ICurrentUser currentUser)
 {
     return node.OwnerId == currentUser.Id;
 }
예제 #7
0
        public async Task<IHttpActionResult> Upload(int id)
        {
            if (!Request.Content.IsMimeMultipartContent("form-data"))
            {
                return StatusCode(HttpStatusCode.UnsupportedMediaType);
            }

            var node = new Node
            {
                Id = id
            };

            await _fileService.UploadFileAsync(Request.Content, node);

            if (!_fileService.State.IsValid)
            {
                return BadRequest(_fileService.State.ErrorMessage);
            }

            return Ok();
        }
예제 #8
0
        public async Task<IHttpActionResult> Post(UploadedNodeViewModel uploadedNode)
        {
            var node = new Node
            {
                Name = uploadedNode.Name,
                ContentType = uploadedNode.ContentType,
                RootId = uploadedNode.RootNodeId,
                Type = uploadedNode.IsFolder ? NodeType.Folder : NodeType.Other,
                Size = uploadedNode.Size
            };

            await _fileService.AddNodeAsync(node);

            if (!_fileService.State.IsValid)
            {
                return BadRequest(_fileService.State.ErrorMessage);
            }

            return Ok(new NodeViewModel(node));
        }
예제 #9
0
        private void RecursiveDelete(Node parent)
        {
            if (parent.Type == NodeType.Folder)
            {
                var siblings = parent.Siblings.ToList();
                foreach (var child in siblings)
                {
                    RecursiveDelete(child);
                }
            }
            else
            {
                parent.Owner.AvailableMemory += parent.Size;
            }

            _nodeRepository.Remove(parent);
        }
예제 #10
0
        public async Task UploadFileAsync(HttpContent content, Node node)
        {
            _nodeRepository.Attach(node);
            var entry = _unitOfWork.Entry(node);

            string path;
            try
            {
                path = await _blobService.UploadFileAsync(content);
            }
            catch (Exception ex)
            {
                entry.State = EntityState.Deleted;
                await _unitOfWork.SaveChangesAsync();
                _modelState.ErrorMessage = ex.Message;
                return;
            }

            node.Path = path;

            entry.Property(f => f.Path).IsModified = true;

            await _unitOfWork.SaveChangesAsync();

            return;
        }