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
}
}
|