Esempio n. 1
0
        public Manager(IDbConnection dbConnection, string rootFolder)
        {
            if (dbConnection == null) throw new ArgumentNullException("dbConnection");
            if (rootFolder == null) throw new ArgumentNullException("rootFolder");

            if (dbConnection.State != ConnectionState.Open) throw new ArgumentException("dbConnection must be in an open state.");

            db = dbConnection;
            this.rootFolder = rootFolder;
            if (!IO.Directory.Exists(rootFolder))
                IO.Directory.CreateDirectory(rootFolder);

            //Validate the root folder exists
            Folder root = GetFolder("/");
            if (root == null)
            {
                root = new Folder()
                {
                    CreatedTimeStamp = DateTime.UtcNow,
                    FullPath = "/",
                    Name = "",
                    IsDeleted = false,
                    IsPurged = false,
                    LastModifiedTimestamp = DateTime.UtcNow,
                    ParentFolderId = null
                };
                db.Insert<Folder>(root);
            }
        }
Esempio n. 2
0
 public Result PurgeFolder(Folder source)
 {
     throw new NotImplementedException();
 }
Esempio n. 3
0
 /// <summary>
 /// Move the contents of the source folder to the target folder.  Target folder must already exist.
 /// </summary>
 /// <param name="source"></param>
 /// <param name="target"></param>
 /// <param name="behavior"></param>
 /// <returns></returns>
 public Result MoveFolder(Folder source, Folder target, OverwriteBehavior behavior)
 {
     return MoveFolderInternal(source, target, behavior, false);
 }
Esempio n. 4
0
        /// <summary>
        /// Copy the contents of one folder to another.  Only tip versions of files are copied.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <param name="behavior"></param>
        /// <returns></returns>
        public Result CopyFolder(Folder source, Folder target, OverwriteBehavior behavior, out Folder newFolder)
        {
            newFolder = null;

            if (source == null) throw new ArgumentNullException("source");
            if (target == null) throw new ArgumentNullException("parentTarget");
            if (source.IsDeleted || target.IsDeleted) return Result.CannotModifyDeletedItems;

            // Validate we aren't copying the source into one of it's childs
            // TODO - is a name match okay, or do we need to actually query out?
            if (target.FullPath.StartsWith(source.FullPath))
            {
                return Result.TargetIsChildOfSource;
            }

            //if (behavior == OverwriteBehavior.Overwrite) throw new ArgumentException("Behavior cannot be overwrite for copy operation.");

            // Create the base folder that we're copying from
            CreateFolder(target, source.Name, OverwriteBehavior.Copy, out newFolder);

            CopyFolderInternal(source, newFolder, behavior);
            return Result.Success;
        }
Esempio n. 5
0
        public Result CopyFile(File source, Folder target, string newFileName, OverwriteBehavior behavior, out File file)
        {
            file = null;
            if (source == null) throw new ArgumentNullException("source");
            if (target == null) throw new ArgumentNullException("target");
            if (string.IsNullOrWhiteSpace(newFileName)) throw new ArgumentNullException("newFileName");
            if (source.IsDeleted || target.IsDeleted) return Result.CannotModifyDeletedItems;

            //See if the file already exists
            file = GetFile(target, newFileName);
            if (file != null)
            {
                if (behavior == OverwriteBehavior.RaiseConflict)
                    return Result.FileAlreadyExists;
                //else if (behavior == OverwriteBehavior.Overwrite)
                //{
                //    // ???
                //}
                else if (behavior == OverwriteBehavior.Copy)
                {
                    do
                    {
                        //Assume the extension is anything after the first ".".  Put the " - Copy" before the file extension.
                        int extensionDot = newFileName.IndexOf('.');
                        newFileName = newFileName.Substring(0, extensionDot) + " - Copy" + newFileName.Substring(extensionDot);
                        file = GetFile(target, newFileName);
                    } while (file != null);
                }
            }

            FileBase newFile = new FileBase()
            {
                Name = newFileName,
                ParentFolderId = target.Id,
                CreatedTimeStamp = DateTime.UtcNow,
                LastModifiedTimestamp = DateTime.UtcNow,
                IsDeleted = false,
                IsPurged = false
            };
            db.Insert<FileBase>(newFile);
            long fileId = db.GetLastInsertId();

            // Point to the tip version of the source file
            db.Insert<FileVersion>(new FileVersion() { FileId = fileId, Version = 1, Size = source.Size, FileStore = source.FileStore, IsCurrent = true, IsDeleted = false });

            file = GetFile(fileId);
            return Result.Success;
        }
Esempio n. 6
0
 /// <summary>
 /// Perform a version-shallow copy of the source file.  The current tip version of the <paramref name="source">source</paramref> file is the tip version
 /// off the new file, and no version history is copied.
 /// </summary>
 /// <param name="source"></param>
 /// <param name="target"></param>
 /// <param name="file"></param>
 /// <returns></returns>
 public Result CopyFile(File source, Folder target, out File file)
 {
     return CopyFile(source, target, OverwriteBehavior.Copy, out file);
 }
Esempio n. 7
0
        private Result MoveFileInternal(File source, Folder target, string newFileName, OverwriteBehavior behavior, bool restoreFile)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (target == null) throw new ArgumentNullException("target");
            if (string.IsNullOrWhiteSpace(newFileName)) throw new ArgumentNullException("newFileName");
            if ((source.IsDeleted && !restoreFile) || target.IsDeleted) return Result.CannotModifyDeletedItems;

            // See if a file of the same name exists at the target
            File file = GetFile(target, newFileName);
            if (file != null)
            {
                if (behavior == OverwriteBehavior.RaiseConflict)
                    return Result.FileAlreadyExists;
                //else if (behavior == OverwriteBehavior.Overwrite)
                //{
                //    // ???
                //}
                else if (behavior == OverwriteBehavior.Copy)
                {
                    do
                    {
                        //Assume the extension is anything after the first ".".  Put the " - Copy" before the file extension.
                        int extensionDot = newFileName.IndexOf('.');
                        newFileName = newFileName.Substring(0, extensionDot) + " - Copy" + newFileName.Substring(extensionDot);
                        file = GetFile(target, newFileName);
                    } while (file != null);
                }
            }

            // Update the file's path & name
            source.Name = newFileName;
            source.LastModifiedTimestamp = DateTime.UtcNow;
            source.ParentFolderId = target.Id;

            bool isDeleted = (restoreFile) ? false : source.IsDeleted;
            db.Update<FileBase>(new { source.Name, source.LastModifiedTimestamp, source.ParentFolderId, IsDeleted = isDeleted }, fb => fb.Id == source.Id);
            return Result.Success;
        }
Esempio n. 8
0
        private void CopyFolderInternal(Folder source, Folder target, OverwriteBehavior behavior)
        {
            IEnumerable<Folder> folders = null;
            IEnumerable<File> files = null;
            GetChildren(source, out folders, out files);
            File dummy;
            foreach (File file in files)
            {
                CopyFile(file, target, out dummy);
            }

            foreach (Folder sourceFolder in folders)
            {
                Folder newTarget;
                CreateFolder(target, sourceFolder.Name, behavior, out newTarget);
                CopyFolderInternal(sourceFolder, newTarget, behavior);
            }
        }
Esempio n. 9
0
        public Result GetChildren(Folder folder, ReadType readType, out IEnumerable<Folder> folders, out IEnumerable<File> files)
        {
            if (folder == null) throw new ArgumentNullException("folder");
            folders = new List<Folder>();
            files = new List<File>();

            SqlExpressionVisitor<Folder> folderEV = OrmLiteConfig.DialectProvider.ExpressionVisitor<Folder>();
            folderEV.Where(f => f.ParentFolderId == folder.Id);
            if (readType == ReadType.Default) folderEV.Where(f => f.IsDeleted == false);
            if (readType == ReadType.OnlyDeleted) folderEV.Where(f => f.IsDeleted == true);
            folders = db.Select<Folder>(folderEV);

            var sql = new StringBuilder(@"
            SELECT f.FileId as Id, f.Name, f.ParentFolderId, f.IsDeleted, f.IsPurged, f.CreatedTimeStamp, f.LastModifiedTimestamp
            ,fv.FileStore, fv.Version as CurrentVersion, fv.Size, d.FullPath + f.Name as FullPath
            FROM [File] f
            INNER JOIN [FileVersion] fv
            ON	fv.FileId = f.FileId
            AND	fv.IsCurrent = 1
            INNER JOIN [Folder] d
            ON  d.FolderId = f.ParentFolderId
            WHERE f.ParentFolderId = {0}".Params(folder.Id));

            if (readType.HasFlag(ReadType.OnlyDeleted)) sql.Append(" AND f.IsDeleted = 1");
            if (readType.HasFlag(ReadType.OnlyNonDeleted)) sql.Append(" AND f.IsDeleted = 0");
            files = db.Query<File>(sql.ToString());

            return Result.Success;
        }
Esempio n. 10
0
 public Result GetChildren(Folder folder, out IEnumerable<Folder> folders, out IEnumerable<File> files)
 {
     return GetChildren(folder, ReadType.Default, out folders, out files);
 }
Esempio n. 11
0
 public Result DeleteFolder(Folder source)
 {
     if (source == null) throw new ArgumentNullException("source");
     source.IsDeleted = true;
     db.Update<Folder>(source);
     return Result.Success;
 }
Esempio n. 12
0
        public Result CreateFolder(Folder parentFolder, string name, OverwriteBehavior behavior, out Folder folder)
        {
            if (parentFolder == null) throw new ArgumentNullException("parentFolder");
            if (string.IsNullOrWhiteSpace(name)) throw new ArgumentNullException("name");

            //Validate parent folder doesn't have a conflicting child
            folder = GetFolder(parentFolder.FullPath + name + "/");
            if (folder != null)
            {
                // If behavior is "skip", we toss an error
                if (behavior == OverwriteBehavior.RaiseConflict)
                {
                    folder = null;
                    return Result.FolderAlreadyExists;
                }
                // Behavior is overwrite, we'll just return the existing folder
                //else if (behavior == OverwriteBehavior.Overwrite)
                //{
                //    return Result.Success;
                //}
                // Behavior is "copy", we find a valid name with "Copy" at the end
                else if (behavior == OverwriteBehavior.Copy)
                {
                    do
                    {
                        name = name + " - Copy";
                        folder = GetFolder(parentFolder.FullPath + name + "/");
                        //if (folder == null) okay = true;
                    } while (folder != null);
                }
            }

            // Folder doesn't exist, we can create it
            folder = new Folder()
            {
                FullPath = parentFolder.FullPath + name + "/",
                Name = name,
                IsDeleted = false,
                IsPurged = false,
                CreatedTimeStamp = DateTime.UtcNow,
                LastModifiedTimestamp = DateTime.UtcNow,
                ParentFolderId = parentFolder.Id
            };

            db.Insert<Folder>(folder);
            folder.Id = db.GetLastInsertId();
            return Result.Success;
        }
Esempio n. 13
0
 public Result CreateFolder(Folder parentFolder, string name, out Folder folder)
 {
     return CreateFolder(parentFolder, name, OverwriteBehavior.RaiseConflict, out folder);
 }
Esempio n. 14
0
        public Result CreateFile(Folder folder, string fileName, IO.Stream stream, out File file)
        {
            file = null;
            Guid fileStoreGuid;
            long fileSize;

            PersistStreamInternal(stream, out fileStoreGuid, out fileSize);

            File existingFile = GetFile(folder, fileName);
            long fileId;
            int nextVersion;

            if (existingFile != null)
            {
                fileId = existingFile.Id;
                nextVersion = GetNextVersion(existingFile);
            }
            else
            {
                nextVersion = 1;
                FileBase newFile = new FileBase()
                {
                    Name = fileName,
                    ParentFolderId = folder.Id,
                    FullPath = folder.FullPath + fileName,
                    CreatedTimeStamp = DateTime.UtcNow,
                    LastModifiedTimestamp = DateTime.UtcNow,
                    IsDeleted = false,
                    IsPurged = false
                };
                db.Insert<FileBase>(newFile);
                fileId = db.GetLastInsertId();
            }

            db.Update<FileVersion>("IsCurrent = 0", "FileId = {0}".Params(fileId));
            db.Insert<FileVersion>(new FileVersion() { FileId = fileId, Version = nextVersion, Size = fileSize, FileStore = fileStoreGuid, IsCurrent = true, IsDeleted = false });

            file = GetFile(fileId);
            return Result.Success;
        }
Esempio n. 15
0
        public Result RenameFolder(Folder source, string name)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (string.IsNullOrWhiteSpace(name)) throw new ArgumentNullException("name");
            if (source.IsDeleted) return Result.CannotModifyDeletedItems;

            string oldFullPath = source.FullPath;
            string newFullPath = GetParentFolderName(oldFullPath) + name + "/";

            if (GetFolder(newFullPath) != null)
                return Result.FolderAlreadyExists;

            foreach (Folder folder in GetChildrenRecursive(source, EnumerateChildTypes.Folders))
            {
                Regex re = new Regex(Regex.Escape(source.FullPath), RegexOptions.IgnoreCase);
                //TODO Do we update the LMT for child folders?
                db.Update<Folder>(new { FullPath = re.Replace(folder.FullPath, newFullPath, 1), LastModifiedTimestamp = DateTime.UtcNow }, f => f.Id == folder.Id);
            }

            source.Name = name;
            source.FullPath = newFullPath;
            source.LastModifiedTimestamp = DateTime.UtcNow;
            db.Update<Folder>(source);

            return Result.Success;
        }
Esempio n. 16
0
        public File GetFile(Folder parentFolder, string fileName)
        {
            if (parentFolder == null) throw new ArgumentNullException("parentFolder");
            if (string.IsNullOrWhiteSpace(fileName) == true) throw new ArgumentNullException("fileName");

            string sql = @"
            SELECT f.FileId as Id, f.Name, f.ParentFolderId, f.IsDeleted, f.IsPurged, f.CreatedTimeStamp, f.LastModifiedTimestamp
            ,fv.FileStore, fv.Version as CurrentVersion, fv.Size, d.FullPath + f.Name as FullPath
            FROM [File] f
            INNER JOIN [FileVersion] fv
            ON	fv.FileId = f.FileId
            AND	fv.IsCurrent = 1
            AND fv.IsDeleted = 0
            INNER JOIN [Folder] d
            ON	d.FolderId = f.ParentFolderId
            WHERE d.FolderId = {0}
              AND f.IsDeleted = 0
              AND f.Name = {1}";

            List<File> files = db.Query<File>(sql.Params(parentFolder.Id, fileName));
            if (files.Count > 1) throw new InvalidOperationException("Multiple files matched in this folder");
            return files.FirstOrDefault();
        }
Esempio n. 17
0
        public Result RestoreFolder(Folder source)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (!source.IsDeleted) return Result.FolderIsNotDeleted;

            Folder targetFolder = GetFolder(source.FullPath);
            if (targetFolder == null)
            {
                source.IsDeleted = false;
                source.LastModifiedTimestamp = DateTime.UtcNow;
                db.Update<Folder>(source);
                return Result.Success;
            }
            else
            {
                // Target exists (was recreated), so we'll move our deleted folder to
                // a new name with - Copy at the end.
                Folder parentFolder = GetFolder(GetParentFolderName(source.FullPath));
                MoveFolderInternal(source, parentFolder, OverwriteBehavior.Copy, true);
                //source.IsDeleted = false;
                //source.LastModifiedTimestamp = DateTime.UtcNow;
                db.Update<Folder>(source);
                return Result.Success;

            }
        }
Esempio n. 18
0
 public Result MoveFile(File source, Folder target)
 {
     return MoveFile(source, target, OverwriteBehavior.RaiseConflict);
 }
Esempio n. 19
0
        private IEnumerable<IHierarchyItem> GetChildrenRecursive(Folder source, EnumerateChildTypes childTypes)
        {
            List<IHierarchyItem> items = new List<IHierarchyItem>();

            IEnumerable<Folder> folders;
            IEnumerable<File> files;
            GetChildren(source, out folders, out files);

            if ((childTypes & EnumerateChildTypes.Files) == EnumerateChildTypes.Files)
            {
                items.AddRange(files);
            }

            if ((childTypes & EnumerateChildTypes.Folders) == EnumerateChildTypes.Folders)
            {
                items.AddRange(folders);
                foreach (Folder folder in folders)
                    items.AddRange(GetChildrenRecursive(folder, childTypes));
            }

            return items;
        }
Esempio n. 20
0
 public Result MoveFile(File source, Folder target, OverwriteBehavior behavior)
 {
     return MoveFile(source, target, source.Name, behavior);
 }
Esempio n. 21
0
        private Result MoveFolderInternal(Folder source, Folder target, OverwriteBehavior behavior, bool restoreFolder)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (target == null) throw new ArgumentNullException("parentTarget");
            if ((source.IsDeleted && !restoreFolder) || target.IsDeleted) return Result.CannotModifyDeletedItems;

            // Validate we aren't copying the source into one of it's childs
            if (target.FullPath.StartsWith(source.FullPath))
            {
                return Result.TargetIsChildOfSource;
            }

            // A move for a folder is just updating the parent folder reference (of the source) and
            // replacing part of the FullPath of any child folders with the new target path
            string oldPath = source.FullPath;
            string newPath = target.FullPath + source.Name + "/";
            string name = source.Name;

            // See if the target full path already exists, do some behavior-specific logic
            Folder targetFolder = GetFolder(newPath);
            if (targetFolder != null)
            {
                if (behavior == OverwriteBehavior.RaiseConflict)
                {
                    return Result.FolderAlreadyExists;
                }
                else if (behavior == OverwriteBehavior.Copy)
                {
                    do
                    {
                        name = name + " - Copy";
                        targetFolder = GetFolder(target.FullPath + name + "/");
                    } while (targetFolder != null);
                    newPath = target.FullPath + name + "/";
                }
            }

            foreach (Folder folder in GetChildrenRecursive(source, EnumerateChildTypes.Folders))
            {
                Regex re = new Regex(Regex.Escape(oldPath), RegexOptions.IgnoreCase);
                db.Update<Folder>(new { FullPath = re.Replace(folder.FullPath, newPath, 1), LastModifiedTimestamp = DateTime.UtcNow }, f => f.Id == folder.Id);
            }

            source.ParentFolderId = target.Id;
            source.FullPath = newPath;
            source.Name = name;
            source.LastModifiedTimestamp = DateTime.UtcNow;
            source.IsDeleted = (restoreFolder) ? false : source.IsDeleted;

            db.Update<Folder>(source);
            return Result.Success;
        }
Esempio n. 22
0
 public Result MoveFile(File source, Folder target, string newFileName, OverwriteBehavior behavior)
 {
     return MoveFileInternal(source, target, newFileName, behavior, false);
 }
Esempio n. 23
0
 public Result CopyFile(File source, Folder target, OverwriteBehavior behavior, out File file)
 {
     return CopyFile(source, target, source.Name, behavior, out file);
 }
Esempio n. 24
0
 public Result MoveFolder(Folder source, Folder target)
 {
     return MoveFolder(source, target, OverwriteBehavior.Copy);
 }
Esempio n. 25
0
        public void Initialize()
        {
            IDbConnection conn = GlobalInit.dbFactory.CreateDbConnection();
            conn.Open();

            manager = new Manager(conn, GlobalInit.rootFolder);
            Assert.IsNotNull(manager);

            root = manager.GetFolder("/");
        }
Esempio n. 26
0
 public Result CopyFolder(Folder source, Folder target, out Folder newFolder)
 {
     return CopyFolder(source, target, OverwriteBehavior.Copy, out newFolder);
 }