コード例 #1
0
        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));
        }
コード例 #2
0
        public ServiceReview Delete(int fileId, User currenUser, IFileUploadSystemData data, bool isAdminAction)
        {
            FileEntity file = null;

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

            string message = string.Empty;

            if ((file as DirectoryEntity) != null)
            {
                var childrenFiles = DFSNestedElementFinder(currenUser, fileId);
                foreach (var element in childrenFiles)
                {
                    this.Data.Files.Delete(element.Id);

                    // If element is a Binary file it must be deleted from the file system too
                    if (element.IsDirectory == false)
                    {
                        FileManipulator.DeleteFile(element.Id, element.Type);
                    }
                }

                message = string.Format("Directory ({0}) deleted successfully!", file.Name);
            }
            else
            {
                var binFile = file as BinaryFile;
                FileManipulator.DeleteFile(binFile.Id, binFile.Type);

                this.Data.Files.Delete(fileId);
                message = string.Format("File ({0}) deleted successfully!", binFile.Name);
            }

            this.Data.SaveChanges();

            return(new ServiceReview(message, file.LocationId, false));
        }
コード例 #3
0
 public FilesController(IFileUploadSystemData data)
     : base(data)
 {
 }
コード例 #4
0
 public SearchController(IFileUploadSystemData data, ISearchServices searchServices)
     : base(data)
 {
     this.searchServices = searchServices;
 }
コード例 #5
0
 public BaseServices(IFileUploadSystemData data)
 {
     this.Data = data;
 }
コード例 #6
0
 public BaseController(IFileUploadSystemData data)
 {
     this.Data = data;
 }
コード例 #7
0
 public SearchServices(IFileUploadSystemData data)
     : base(data)
 {
 }
コード例 #8
0
 public AdminFilesController(IFileUploadSystemData data, ISearchServices searchServices, IUploadServices uploadServices)
     : base(data)
 {
     this.searchServices = searchServices;
     this.uploadServices = uploadServices;
 }
コード例 #9
0
 public UploadController(IFileUploadSystemData data, IUploadServices uploadServices)
     : base(data)
 {
     this.uploadServices = uploadServices;
 }
コード例 #10
0
        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));
        }
コード例 #11
0
        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);
        }