[FileTools, SimpleSort] Port ConvertFiles over to library

This commit is contained in:
Matt Nadareski
2016-10-20 17:33:25 -07:00
parent e67b85e901
commit 6ced4966d6
5 changed files with 166 additions and 162 deletions

View File

@@ -921,158 +921,5 @@ namespace SabreTools.Helper
return success;
}
/// <summary>
/// Process inputs and convert to TorrentZip or TorrentGZ, optionally converting to Romba
/// </summary>
/// <returns>True if processing was a success, false otherwise</returns>
public bool Convert()
{
bool success = true;
// First, check that the output directory exists
if (!Directory.Exists(_outDir))
{
Directory.CreateDirectory(_outDir);
_outDir = Path.GetFullPath(_outDir);
}
// Then create or clean the temp directory
if (!Directory.Exists(_tempDir))
{
Directory.CreateDirectory(_tempDir);
}
else
{
FileTools.CleanDirectory(_tempDir);
}
// Now process all of the inputs
foreach (string input in _inputs)
{
_logger.User("Examining file " + input);
// Get if the file should be scanned internally and externally
bool shouldExternalProcess, shouldInternalProcess;
ArchiveTools.GetInternalExternalProcess(input, _archiveScanLevel, _logger, out shouldExternalProcess, out shouldInternalProcess);
// Do an external scan of the file, if necessary
if (shouldExternalProcess)
{
// If a DAT is defined, we want to make sure that this file is not in there
Rom rom = FileTools.GetFileInfo(input, _logger);
if (_datdata != null && _datdata.Files.Count > 0)
{
if (rom.HasDuplicates(_datdata, _logger))
{
_logger.User("File '" + input + "' existed in the DAT, skipping...");
continue;
}
}
_logger.User("Processing file " + input);
if (_tgz)
{
success &= ArchiveTools.WriteTorrentGZ(input, _outDir, _romba, _logger);
}
else
{
success &= ArchiveTools.WriteToArchive(input, _outDir, rom, _logger);
}
}
// Process the file as an archive, if necessary
if (shouldInternalProcess)
{
// Now, if the file is a supported archive type, also run on all files within
bool encounteredErrors = ArchiveTools.ExtractArchive(input, _tempDir, _archiveScanLevel, _logger);
// If no errors were encountered, we loop through the temp directory
if (!encounteredErrors)
{
_logger.Verbose("Archive found! Successfully extracted");
foreach (string file in Directory.EnumerateFiles(_tempDir, "*", SearchOption.AllDirectories))
{
// If a DAT is defined, we want to make sure that this file is not in there
Rom rom = FileTools.GetFileInfo(file, _logger);
if (_datdata != null && _datdata.Files.Count > 0)
{
if (rom.HasDuplicates(_datdata, _logger))
{
_logger.User("File '" + file + "' existed in the DAT, skipping...");
continue;
}
}
_logger.User("Processing file " + input);
if (_tgz)
{
success &= ArchiveTools.WriteTorrentGZ(file, _outDir, _romba, _logger);
}
else
{
success &= ArchiveTools.WriteToArchive(file, _outDir, rom, _logger);
}
}
FileTools.CleanDirectory(_tempDir);
}
}
// Delete the source file if we're supposed to
if (_delete)
{
try
{
_logger.User("Attempting to delete " + input);
File.Delete(input);
}
catch (Exception ex)
{
_logger.Error(ex.ToString());
success &= false;
}
}
}
// Now one final delete of the temp directory
while (Directory.Exists(_tempDir))
{
try
{
Directory.Delete(_tempDir, true);
}
catch
{
continue;
}
}
// If we're in romba mode and the size file doesn't exist, create it
if (_romba && !File.Exists(Path.Combine(_outDir, ".romba_size")))
{
// Get the size of all of the files in the output folder
long size = 0;
foreach (string file in Directory.EnumerateFiles(_outDir, "*", SearchOption.AllDirectories))
{
FileInfo tempinfo = new FileInfo(file);
size += tempinfo.Length;
}
// Write out the value to each of the romba depot files
StreamWriter tw = new StreamWriter(File.Open(Path.Combine(_outDir, ".romba_size"), FileMode.Create, FileAccess.Write));
StreamWriter twb = new StreamWriter(File.Open(Path.Combine(_outDir, ".romba_size.backup"), FileMode.Create, FileAccess.Write));
tw.Write(size);
twb.Write(size);
tw.Dispose();
twb.Dispose();
}
return success;
}
}
}