public ServiceReview Rename(int fileId, string newName, User currenUser, IFileUploadSystemData data, bool isAdminAction)
        {
            var    errorFound = true;
            string message    = null;

            FileEntity file = null;

            if (isAdminAction)
            {
                file = data.Files.GetById(fileId);
            }
            else
            {
                file = currenUser.Files.Where(i => i.Id == fileId).FirstOrDefault();
            }

            int containingDirectoryId = file.LocationId;

            if (newName == null || newName.Trim().Length == 0)
            {
                message = "Bad name!";
            }
            else if (file.Name == newName)
            {
                message = "Cannot rename to the same name!";
            }
            else if (CommonFunc.SameFileExistInSameDir(newName, containingDirectoryId, currenUser))
            {
                message = "File with that name already exists in same directory!";
            }
            else
            {
                // If we are changing the file type - .jpg to.png
                // To enable - FileManipulator must rename the file on disk too, or remove all file types
                var binFile = file as BinaryFile;
                if (binFile != null)
                {
                    var newType       = Path.GetExtension(newName);
                    var areTypesMatch = newType.ToLower() == binFile.Type.ToLower();
                    if (!areTypesMatch)
                    {
                        message = string.Format("Changing file types is currently forbidden ({0} to {1})!", binFile.Type, newType);
                        return(new ServiceReview(message, containingDirectoryId, errorFound));
                    }
                }

                var oldName = file.Name;
                file.Name = newName;

                data.Context.Entry(file).State = EntityState.Modified;
                data.SaveChanges();

                errorFound = false;
                message    = string.Format("File ({0}) renamed to ({1})!", oldName, newName);
            }

            return(new ServiceReview(message, containingDirectoryId, errorFound));
        }
        public ServiceReview UploadFile(HttpPostedFileBase uploadFile, int?currentDirectory, User currenUser, IFileUploadSystemData data)
        {
            var res = new ServiceReview();
            var currentDirectoryId = currentDirectory ?? -1;

            res.RedirectId = currentDirectory;
            var errorFound = false;

            if (uploadFile == null ||
                uploadFile.ContentLength == 0 ||
                uploadFile.ContentLength > GlobalConstants.MaxUploadFileSize ||
                uploadFile.FileName.Trim().Length < 1)
            {
                res.Message    = "Bad file name/size!";
                res.ErrorFound = true;
                return(res);
            }

            var fileName      = Path.GetFileName(uploadFile.FileName);
            var fileExtension = Path.GetExtension(uploadFile.FileName);

            // Check if the same filename allready exists in current directory
            errorFound = CommonFunc.SameFileExistInSameDir(fileName, currentDirectoryId, currenUser);
            if (errorFound)
            {
                res.ErrorFound = true;
                res.Message    = "A file with that name already exists in the current directory!";
                return(res);
            }

            var newFile = new BinaryFile
            {
                Name       = fileName,
                Type       = fileExtension,
                Size       = uploadFile.ContentLength,
                Owner      = currenUser,
                OwnerId    = currenUser.Id,
                LocationId = currentDirectoryId
            };

            if (currentDirectory != -1)
            {
                ((DirectoryEntity)currenUser.Files.Where(f => f.Id == currentDirectory).FirstOrDefault()).FilesContained.Add(newFile);
            }
            else
            {
                currenUser.Files.Add(newFile);
            }

            data.SaveChanges();
            int id = newFile.Id;

            FileManipulator.UploadFile(uploadFile, id, fileExtension);

            res.Message = string.Format("File ({0}) uploaded successfully!", fileName);
            return(res);
        }
        public ServiceReview CreateDirectory(int parentDirectoryId, string directoryName, User currenUser, IFileUploadSystemData data)
        {
            var    errorFound = false;
            string message    = null;

            if (!(directoryName != null && directoryName.Length > 0))
            {
                errorFound = true;
                message    = "Bad directory name!";
            }
            else if (CommonFunc.SameFileExistInSameDir(directoryName, parentDirectoryId, currenUser))
            {
                errorFound = true;
                message    = "Dublicate names in same directory!";
            }
            else
            {
                var newDirectory = new DirectoryEntity
                {
                    Name       = directoryName,
                    LocationId = parentDirectoryId,
                    Owner      = currenUser,
                    OwnerId    = currenUser.Id
                };

                if (parentDirectoryId == -1)
                {
                    currenUser.Files.Add(newDirectory);
                }
                else
                {
                    ((DirectoryEntity)(currenUser.Files
                                       .Where(f => f.Id == parentDirectoryId)
                                       .FirstOrDefault()))
                    .FilesContained.Add(newDirectory);
                }

                data.SaveChanges();
            }

            if (!errorFound)
            {
                message = "Directory (" + directoryName + ") created!";
            }

            return(new ServiceReview(message, parentDirectoryId, errorFound));
        }