using System; using System.Collections.Generic; using System.IO; using System.Linq; using log4net; using Microsoft.Practices.ServiceLocation; using Taloyhtio.GeneralApi.Common.Extensions; using Taloyhtio.GeneralApi.IntegrationUtility.Common; namespace Taloyhtio.GeneralApi.IntegrationUtility.Repositories.Impl { public class FileRepository : IFileRepository { private ILog logger = ServiceLocator.Current.GetInstance(); // private class Comparer : IComparer // { // public int Compare(FileInfo x, FileInfo y) // { // return DateTime.Compare(x.LastWriteTime, y.LastWriteTime); // } // } public List GetAllNew(string folderPath) { try { if (string.IsNullOrEmpty(folderPath)) { return new List(); } // 2010-06-11: it is important to get files in last write date order return Directory.GetFiles(folderPath).ToList(); // var dirInfo = new DirectoryInfo(folderPath); // var files = dirInfo.GetFiles(); // if (files.IsNullOrEmpty()) // { // return new List(); // } // Array.Sort(files, new Comparer()); // return files.Select(f => f.FullName).ToList(); } catch (Exception x) { this.logger.ErrorFormat(string.Format("{0}\n{1}", x.Message, x.StackTrace)); return new List(); } } public string GetContent(string fileName) { try { if (string.IsNullOrEmpty(fileName)) { return null; } if (!File.Exists(fileName)) { return null; } return File.ReadAllText(fileName); } catch (Exception x) { this.logger.ErrorFormat(string.Format("{0}\n{1}", x.Message, x.StackTrace)); return null; } } public bool Move(string targetPath, string destPath) { try { if (string.IsNullOrEmpty(targetPath)) { this.logger.ErrorFormat("Target path is null or empty"); return false; } if (string.IsNullOrEmpty(destPath)) { this.logger.ErrorFormat("Destination path is null or empty"); return false; } if (!File.Exists(targetPath)) { this.logger.ErrorFormat("Target file '{0}' not found", targetPath); return false; } if (File.Exists(destPath)) { this.logger.ErrorFormat("Destination file '{0}' with the same name alreayd exists", destPath); return false; } string destDir = Path.GetDirectoryName(destPath); FileUtilities.EnsureFolderExists(destDir); File.Move(targetPath, destPath); return true; } catch (Exception x) { this.logger.ErrorFormat(string.Format("Error occured during moving of file '{0}' to '{1}':\n{2}\n{3}", destPath, targetPath, x.Message, x.StackTrace)); return false; } } } }