Пример #1
0
        /// <summary>
        /// 同步远端更改到本地
        /// </summary>
        public static void SysCloudToLocal()
        {
            while (true)
            {
                try
                {
                    if (!MemoryFileManager.GetInstance().IsNeedSync())
                    {
                        CloudFiles = new List <string>();

                        var fileInfo = new CloudFileInfoModel {
                            Path = "/", IsDir = true
                        };

                        SyncCloudFileToLocal(fileInfo);

                        SyncCloudRemoveFileToLocal();
                    }

                    Thread.Sleep(15 * 1000);
                }
                catch
                {
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 同步删除的内容到云
        /// </summary>
        public static void SyncRemoveFileToCloud()
        {
            var memFiles = new List <MemoryFile>();

            memFiles =
                MemoryFileManager.GetInstance().GetAllFiles().OrderBy(p => p.FilePath).Where(
                    p => p.FileStatus == FileStatusEnum.Remove).ToList();
            var cloudDisk = new CloudDiskManager();

            if (memFiles.Count > 0)
            {
                while (memFiles.Count > 0)
                {
                    try
                    {
                        var memFile = memFiles[0];
                        if (memFile.FileType == FileTypeEnum.Directory)
                        {
                            cloudDisk.DeleteDirectory(memFile.FilePath);
                            memFiles.RemoveAll(p => p.FilePath.Contains(memFile.FilePath));
                            MemoryFileManager.GetInstance().RemoveFile(memFile.FilePath);
                        }
                        else if (memFile.FileType == FileTypeEnum.File)
                        {
                            cloudDisk.DeleteFile(CloudDiskType.NOT_SPECIFIED, memFile.FilePath);
                            memFiles.RemoveAll(p => p.FilePath.Contains(memFile.FilePath));
                            MemoryFileManager.GetInstance().RemoveFile(memFile.FilePath);
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }
Пример #3
0
 public int WriteFile(string filename, byte[] buffer, ref uint writtenBytes, long offset, DokanFileInfo info)
 {
     try
     {
         var path = this.GetPath(filename);
         if (!File.Exists(path))
         {
             return(DokanNet.ERROR_FILE_NOT_FOUND);
         }
         File.WriteAllBytes(path, buffer);
         var file = MemoryFileManager.GetInstance().GetFile(path);
         if (file != null && file.FileStatus != FileStatusEnum.Create && file.FileStatus != FileStatusEnum.Remove)
         {
             var fileInfo   = new FileInfo(path);
             var memoryFile = new MemoryFile
             {
                 CreateDate     = fileInfo.CreationTime,
                 FilePath       = path.Replace(this.root_, ""),
                 FileStatus     = FileStatusEnum.Append,
                 FileType       = FileTypeEnum.File,
                 LastModifyDate = fileInfo.LastWriteTime
             };
             MemoryFileManager.GetInstance().SetFile(memoryFile);
         }
         return(DokanNet.DOKAN_SUCCESS);
     }
     catch (Exception)
     {
         return(DokanNet.DOKAN_SUCCESS);
     }
 }
Пример #4
0
        public int DeleteDirectory(string filename, DokanFileInfo info)
        {
            try
            {
                if (filename == "\\")
                {
                    return(DokanNet.DOKAN_SUCCESS);
                }
                var path = this.GetPath(filename);
                if (Directory.Exists(path))
                {
                    var dir        = new DirectoryInfo(path);
                    var memoryFile = new MemoryFile
                    {
                        CreateDate     = dir.CreationTime,
                        FilePath       = path.Replace(this.root_, ""),
                        FileStatus     = FileStatusEnum.Remove,
                        FileType       = FileTypeEnum.Directory,
                        LastModifyDate = dir.LastWriteTime
                    };

                    Directory.Delete(path, true);

                    MemoryFileManager.GetInstance().SetFile(memoryFile);
                }

                return(DokanNet.DOKAN_SUCCESS);
            }
            catch (Exception)
            {
                return(DokanNet.DOKAN_SUCCESS);
            }
        }
Пример #5
0
        public int DeleteFile(string filename, DokanFileInfo info)
        {
            try
            {
                var path = this.GetPath(filename);
                if (File.Exists(path))
                {
                    var fileInfo   = new FileInfo(path);
                    var memoryFile = new MemoryFile
                    {
                        CreateDate     = fileInfo.CreationTime,
                        FilePath       = path.Replace(this.root_, ""),
                        FileStatus     = FileStatusEnum.Remove,
                        FileType       = FileTypeEnum.File,
                        LastModifyDate = fileInfo.LastWriteTime
                    };

                    File.Delete(path);

                    MemoryFileManager.GetInstance().SetFile(memoryFile);
                }
                return(DokanNet.DOKAN_SUCCESS);
            }
            catch (Exception)
            {
                return(DokanNet.DOKAN_SUCCESS);
            }
        }
Пример #6
0
        /// <summary>
        /// 同步本地更改到云端
        /// </summary>
        public static void CheckFile()
        {
            while (true)
            {
                try
                {
                    if (MemoryFileManager.GetInstance().IsNeedSync() ||
                        MemoryFileManager.GetInstance().GetAllFiles().Count == 0)    //如果缓存有更新
                    {
                        SysToCloud();
                    }

                    Thread.Sleep(5 * 1000);
                }
                catch
                {
                }
            }
        }
Пример #7
0
        /// <summary>
        /// 同步更改的内容到云
        /// </summary>
        public static void SyncChangeFileToCloud()
        {
            var memFiles = new List <MemoryFile>();

            memFiles =
                MemoryFileManager.GetInstance().GetAllFiles().OrderBy(p => p.FilePath).Where(
                    p => p.FileStatus != FileStatusEnum.Remove).ToList();
            var cloudDisk = new CloudDiskManager();
            var root      = LocalDiskPathHelper.GetPath();

            if (memFiles.Count > 0)
            {
                while (memFiles.Count > 0)
                {
                    var memFile = memFiles[0];
                    try
                    {
                        if (memFile.FileType == FileTypeEnum.Directory)
                        {
                            if (memFile.FileStatus == FileStatusEnum.Create)
                            {
                                cloudDisk.CreateDirectory(memFile.FilePath);
                                memFile.FileStatus = FileStatusEnum.Normal;
                                MemoryFileManager.GetInstance().SetFile(memFile);
                            }
                            else
                            {
                                var cloudFiles = cloudDisk.GetCloudFileInfo(CloudDiskType.NOT_SPECIFIED, true, memFile.FilePath);
                                if (cloudFiles == null)
                                {
                                    cloudDisk.CreateDirectory(memFile.FilePath);
                                }
                            }
                        }
                        else if (memFile.FileType == FileTypeEnum.File)
                        {
                            if (memFile.FileStatus == FileStatusEnum.Create)
                            {
                                var fileInfo = new FileInfo(root + memFile.FilePath);
                                var buffer   = new byte[fileInfo.Length];
                                fileInfo.OpenRead().Read(buffer, 0, (int)fileInfo.Length);
                                cloudDisk.UploadFile(
                                    CloudFileUploadType.Create, memFile.FilePath, buffer);
                                memFile.FileStatus = FileStatusEnum.Normal;
                                MemoryFileManager.GetInstance().SetFile(memFile);
                            }
                            else
                            {
                                var cloudFiles = cloudDisk.GetCloudFileInfo(CloudDiskType.NOT_SPECIFIED, false, memFile.FilePath);
                                if (cloudFiles == null)
                                {
                                    var fileInfo = new FileInfo(root + memFile.FilePath);
                                    var buffer   = new byte[fileInfo.Length];
                                    fileInfo.OpenRead().Read(buffer, 0, (int)fileInfo.Length);
                                    cloudDisk.UploadFile(
                                        CloudFileUploadType.Create, memFile.FilePath, buffer);
                                }
                                else
                                {
                                    var fileInfo = new FileInfo(root + memFile.FilePath);
                                    if (cloudFiles.LastModifiedDate.HasValue)
                                    {
                                        if (fileInfo.LastWriteTime > cloudFiles.LastModifiedDate)
                                        {
                                            var buffer = new byte[fileInfo.Length];
                                            fileInfo.OpenRead().Read(buffer, 0, (int)fileInfo.Length);
                                            cloudDisk.UploadFile(
                                                CloudFileUploadType.Create, memFile.FilePath, buffer);
                                        }
                                    }
                                }
                            }
                        }
                        memFiles.Remove(memFile);
                    }
                    catch (Exception)
                    {
                        memFiles.Remove(memFile);
                    }
                }
            }
        }
Пример #8
0
        public int MoveFile(string filename, string newname, bool replace, DokanFileInfo info)
        {
            try
            {
                if (filename == "\\")
                {
                    return(DokanNet.DOKAN_SUCCESS);
                }
                var newPath = this.GetPath(newname);
                var path    = this.GetPath(filename);

                if (string.IsNullOrEmpty(Path.GetExtension(newPath)))
                {
                    var dir = new DirectoryInfo(path);

                    var oldFiles =
                        MemoryFileManager.GetInstance().GetAllFiles().Where(p => p.FilePath.Contains(filename)).ToList();

                    Directory.Move(path, newPath);

                    var memoryFile = new MemoryFile
                    {
                        CreateDate     = dir.CreationTime,
                        FilePath       = dir.FullName.Replace(this.root_, ""),
                        FileStatus     = FileStatusEnum.Remove,
                        FileType       = FileTypeEnum.Directory,
                        LastModifyDate = dir.LastWriteTime
                    };

                    MemoryFileManager.GetInstance().SetFile(memoryFile);

                    foreach (var oldFile in oldFiles)
                    {
                        var memoryFileNew = new MemoryFile()
                        {
                            CreateDate     = oldFile.CreateDate,
                            FilePath       = oldFile.FilePath.Replace(filename, newname),
                            FileStatus     = FileStatusEnum.Create,
                            FileType       = oldFile.FileType,
                            LastModifyDate = oldFile.LastModifyDate
                        };
                        MemoryFileManager.GetInstance().SetFile(memoryFileNew);
                    }
                }
                else
                {
                    var fileOld    = new FileInfo(path);
                    var memoryFile = new MemoryFile
                    {
                        CreateDate     = fileOld.CreationTime,
                        FilePath       = fileOld.FullName.Replace(this.root_, ""),
                        FileStatus     = FileStatusEnum.Remove,
                        FileType       = FileTypeEnum.File,
                        LastModifyDate = fileOld.LastWriteTime
                    };

                    File.Move(path, newPath);

                    var fileNew = new FileInfo(newPath);

                    var memoryFileNew = new MemoryFile
                    {
                        CreateDate     = fileNew.CreationTime,
                        FilePath       = fileNew.FullName.Replace(this.root_, ""),
                        FileStatus     = FileStatusEnum.Create,
                        FileType       = FileTypeEnum.File,
                        LastModifyDate = fileNew.LastWriteTime
                    };

                    MemoryFileManager.GetInstance().SetFile(memoryFile);

                    MemoryFileManager.GetInstance().SetFile(memoryFileNew);
                }

                return(DokanNet.DOKAN_SUCCESS);
            }
            catch (Exception)
            {
                return(DokanNet.DOKAN_SUCCESS);
            }
        }
Пример #9
0
        public int CreateFile(string filename, FileAccess access,
                              FileShare share, FileMode mode, FileOptions options, DokanFileInfo info)
        {
            try
            {
                string path = GetPath(filename);
                if (Directory.Exists(path))
                {
                    info.IsDirectory = true;
                    return(DokanNet.DOKAN_SUCCESS);
                }
                switch (mode)
                {
                case FileMode.Append:
                    return(DokanNet.DOKAN_SUCCESS);

                case FileMode.Create:
                    if (!File.Exists(path))
                    {
                        FileStream fs = File.Create(path);
                        fs.Close();

                        var fileInfo   = new FileInfo(path);
                        var memoryFile = new MemoryFile
                        {
                            CreateDate     = fileInfo.CreationTime,
                            FilePath       = path.Replace(this.root_, ""),
                            FileStatus     = FileStatusEnum.Create,
                            FileType       = FileTypeEnum.File,
                            LastModifyDate = fileInfo.LastWriteTime
                        };
                        MemoryFileManager.GetInstance().SetFile(memoryFile);
                    }
                    return(DokanNet.DOKAN_SUCCESS);

                case FileMode.CreateNew:
                    if (!File.Exists(path))
                    {
                        FileStream fs = File.Create(path);
                        fs.Close();

                        var fileInfo   = new FileInfo(path);
                        var memoryFile = new MemoryFile
                        {
                            CreateDate     = fileInfo.CreationTime,
                            FilePath       = path.Replace(this.root_, ""),
                            FileStatus     = FileStatusEnum.Create,
                            FileType       = FileTypeEnum.File,
                            LastModifyDate = fileInfo.LastWriteTime
                        };
                        MemoryFileManager.GetInstance().SetFile(memoryFile);
                    }
                    return(DokanNet.DOKAN_SUCCESS);

                case FileMode.Open:
                    return(DokanNet.DOKAN_SUCCESS);

                case FileMode.OpenOrCreate:
                    return(DokanNet.DOKAN_SUCCESS);
                }
                return(-DokanNet.ERROR_FILE_NOT_FOUND);
            }
            catch
            {
                return(DokanNet.DOKAN_SUCCESS);
            }
        }