FileTransferManager.cs :  » Network-Clients » iReaper » IReaper » FileUtils » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » iReaper 
iReaper » IReaper » FileUtils » FileTransferManager.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.ComponentModel;
using IReaper.Running;
using IReaper.CourseData;
using IReaper.FileData;
using IReaper.PathManager;

namespace IReaper.FileUtils{

    /// <summary>
    /// 
    /// </summary>
    /// <param name="File"></param>
    /// <param name="SrcPath"></param>
    /// <param name="DesPath"></param>
    public delegate void FileTransferDelegate(CourseFileData File,string SrcPath,string DesPath);
    /// <summary>
    /// 
    /// </summary>
    public class FileTransferManager
    {
        #region Export Files
        public static void TransferCourseFileData(  CourseFileData[] files,
                                                    PathPolicyType policy,
                                                    bool overwrite,
                                                    string rootFolderPath,
                                                    OperationType operation,
                                                    FileTransferDelegate onOneFileTransfered
                                                )
        { 
            //validate rootFolder
            if(string.IsNullOrEmpty(rootFolderPath) || !Directory.Exists(rootFolderPath))
            {
                throw new System.IO.DirectoryNotFoundException();
            }
            //loop the files
            foreach (CourseFileData file in files)
            { 
                //crate path
                string relativePath = Manager.GetRelativePath(file,policy);
                string newFolder = Path.Combine(rootFolderPath, relativePath);
                //check folder existence
                if (!Directory.Exists(newFolder))
                {
                    Directory.CreateDirectory(newFolder);
                }
                //execute the opera
                if (OperateOneFile(file, newFolder, true, operation))
                {
                    //invoke the notification
                    if (onOneFileTransfered != null)
                    {
                        onOneFileTransfered.Invoke(file, file.FilePath, newFolder);
                    }
                    //,
                    file.Storage.RootPath = relativePath;
                    file.ReloadPathFromStorage();
                }
                
            }
        }

        /// <summary>
        /// CourseFileData
        /// </summary>
        /// <param name="file">CourseFileData</param>
        /// <param name="des"></param>
        /// <param name="overwrite"></param>
        /// <param name="operation"></param>
        private static bool OperateOneFile( CourseFileData file,
                                            string des,
                                            bool overwrite, 
                                            OperationType operation)
        {
            //get the path
            string desFullPath = Path.Combine(des, file.FileName);
            //if the src and des are equivlant, return;
            if (desFullPath.ToLower() == file.FilePath)
            {
                return false;
            }
            if (!File.Exists(file.FilePath))
            {
                return false;
            }
            //make the operation
            if (operation == OperationType.Move)
            {
                if (File.Exists(des))
                {
                    if (overwrite)
                    {
                        File.Delete(des);
                    }
                    else
                    {
                        return false;
                    }
                }
                //move 
                
                File.Move(file.FilePath, Path.Combine(des,file.FileName));
                VacaumFolder(file.RootPath);
                
            }
            else if (operation == OperationType.Copy)
            {
                File.Copy(file.FilePath, desFullPath, overwrite);
            }
            return true;
        
        }

        /* 
         *  1. 
         *  2. 
         *  3. 
         */
        private static void VacaumFolder(string folderPath)
        {
            DirectoryInfo dir = new DirectoryInfo(folderPath);
            DirectoryInfo parent;
            while (dir.GetFileSystemInfos().Length == 0)
            {
                dir.Delete();
                dir = dir.Parent;
            }
            return;
        }
        #endregion

        #region Import Files
        
        /// <summary>
        /// exportLog
        /// </summary>
        /// <param name="ExportLogPath"></param>
        public static void ImportCourseFileDataFromExportedFolder(string ExportLogPath,
                                                                  bool overwrite,
                                                                  ProgressChangedEventHandler OnProgress)
        {
            FileInfo fileInfo = new FileInfo(ExportLogPath);
            if (!fileInfo.Exists)
                throw new FileNotFoundException("Please input valide .export file", ExportLogPath);

            //
             Dictionary<string,Course> keys = CourseDataManager.AllCourses.KeydList;
            //root
            string rootPath = fileInfo.DirectoryName;
            CourseFileDataStorage[] files = CourseFileDataManager.LoadExportList(ExportLogPath);
            for(int i = 0;i < files.Length;i++)
            { 
                CourseFileDataStorage storage = files[i];
                //Course
                string id = storage.ID;
                //
                if (!keys.ContainsKey(id))
                { continue; }
                Course course = keys[id];
                CourseFileData cFile = null;
                //
                switch (storage.Type)
                { 
                    case FileType.Code:
                        cFile = course.Code;
                        break;
                    case FileType.PPT:
                        cFile = course.PPT;
                        break;
                    case FileType.QA:
                        cFile = course.QA;
                        break;
                    case FileType.Video:
                        cFile = course.Video;
                        break;
                    case FileType.Zune:
                        cFile = course.WMV;
                        break;
                    case FileType.MP4:
                        cFile = course.MP4;
                        break;
                    case FileType.MP3:
                        cFile = course.MP3;
                        break;
                }
                //,
                if (cFile == null)
                    continue;
              
                string srcPath = Path.Combine(rootPath, storage.FilePath);
                cFile.FileName = storage.FileName;
                string desPath = cFile.FilePath;
                cFile.RunState = RunningStatue.Finished;
                cFile.LifetimeStatue = LifetimePosition.DownloadProcessed;
                //
                if (!overwrite && File.Exists(desPath))
                    continue;
                
                //
                if (OnProgress != null)
                {
                    int progress = (int)(i * 100 / files.Length);
                    ProgressChangedEventArgs args = new ProgressChangedEventArgs(progress, cFile);
                    OnProgress(null, args);
                }
                //storage
                if (!Directory.Exists(cFile.RootPath))
                {
                    Directory.CreateDirectory(cFile.RootPath);
                }
     
                File.Copy(srcPath, desPath, overwrite);
                //
                CourseFileDataManager.ApplyNewStorage(ref cFile.Storage);
                //
                cFile.Storage.Flush();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="file"></param>
        /// <param name="path"></param>
        public static void ImportCourseFileDataFromSingleFile(CourseFileData file, string path)
        {
            //check
            FileInfo fileInfo = new FileInfo(path);
            if(!fileInfo.Exists)
                return;
            //
            if (file.RunState == RunningStatue.Created)
            {
                CourseFileDataManager.ApplyNewStorage(ref file.Storage);
            }
            //copy
            //generate path
            string relativePath = Manager.GetRelativePath(file);
            string fullpath = Path.Combine(Properties.Settings.Default.RootPath,relativePath);
            string fullName = Path.Combine(fullpath, fileInfo.Name);
            //check duplication
            if (fullName.ToLower() != fileInfo.FullName.ToLower())
            {
                //check folder
                if (!Directory.Exists(fullpath))
                { Directory.CreateDirectory(fullpath); }
                //copy
                fileInfo.CopyTo(fullName, true);
            }
            //change record
            file.Storage.FileName = fileInfo.Name;
            file.Storage.RootPath = relativePath;
            file.RunState = RunningStatue.Finished;
            file.LifetimeStatue = LifetimePosition.DownloadProcessed;
            file.ReloadPathFromStorage();
            //save
        }

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.