/// <summary> /// Constructor for Submission Candidate /// </summary> /// <param name="path">Path to the candidate</param> /// <param name="depth">Current filesystem depth</param> /// <param name="parent">Any parent submission</param> public SubmissionCandidate(string path, int depth, SubmissionCandidate parent) { _guid = Guid.NewGuid(); _guidShorthand = _guid.ToString("N").Substring(0, 6); _resolution = SubmissionCandidateResolution.None; _depth = depth; _path = path; _parent = parent; _fileType = FileTypes.Types.None; _resolvePath = string.Empty; // Initial set of naming information, before processing or exploding, useful // for any sort of filtering that gets done prior to exploding _fileName = Path.GetFileName(path); _fileNameWithoutExtension = Path.GetFileNameWithoutExtension(_fileName); _fileExtension = Path.GetExtension(_fileName); }
/// <summary> /// Export the file for processing, that if it is in an archive. Additionally /// final checks on the file so they are ready to become a submission /// </summary> protected void ExportFile(string workingDirectory, bool ignoreParent = false, bool onlySubmissions = false) { if (IsExported) { return; } _resolution |= SubmissionCandidateResolution.Exported; if (ignoreParent == false && Parent != null) { Parent.ExportFile(workingDirectory, ignoreParent, onlySubmissions); } if (IsArchive && _archiveChildren != null) { // Design path here. Using parents if any. string basePath = string.Empty; if (HasParent) { // archive in a folder if (Parent.IsDirectory) { basePath = Path.Combine(workingDirectory, HunterConfig.GalileoDefaultDataFolder); } // archive in a archive else { basePath = Parent.ReadPath; } } else { basePath = Path.Combine(workingDirectory, HunterConfig.GalileoDefaultDataFolder); } //string basePath = Path.Combine(workingDirectory, HunterConfig.GalileoDefaultDataFolder); _resolvePath = Path.Combine(basePath, string.Format("A_{0}_{1}", GuidShorthand, FileName)); // Create folder here. Directory.CreateDirectory(ReadPath); if (onlySubmissions == false) { foreach (var child in _archiveChildren) { if (child.IsIgnoredButExport) { child.ExportFile(ReadPath, true, onlySubmissions); } } } } if ((IsSubmission || IsIgnoredButExport) && !IsFiltered) { // @TODO // If in an archive, then export file to readpath. if (IsArchivedFile) { _fileName = System.IO.Path.GetFileName(ReadPath); _fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(ReadPath); _fileExtension = System.IO.Path.GetExtension(ReadPath); string[] paths = Platform.SplitArchivePath(ReadPath); paths = Platform.CleanArchivePaths(paths); string exportPath = Path.Combine(workingDirectory, Path.Combine(paths)); Directory.CreateDirectory(Path.GetDirectoryName(new DirectoryInfo(exportPath).FullName)); ArchiveInfo.Extract(ReadPath, exportPath); _fileInfo = new System.IO.FileInfo(exportPath); _resolvePath = exportPath; } else { _fileInfo = new System.IO.FileInfo(ReadPath); // Update with new information based on finalized pathing information _fileName = System.IO.Path.GetFileName(ReadPath); _fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(ReadPath); _fileExtension = System.IO.Path.GetExtension(ReadPath); } _containerPath = Parent.ContainerPath; } }
/// <summary> /// Resolve this candidate and work out what it is; A File, Directory or Archive. /// Once that it is checked against the filters to see if it can be processed /// any futher. /// /// If it's a file then the type is worked out. /// If it's a directory or archive, then it may be 'exploded' so sub-directories /// and files will be added as candidates. /// /// After all of that _resolution contains the resolved state of the Candidate. /// </summary> /// <param name="candidates">Candidates currently being resolved</param> /// <returns></returns> public SubmissionCandidateResolution Resolve(SubmissionCandidates candidates, string workingDirectory) { #if DEBUG Console.WriteLine(ReadPath); #endif // Start off with nothing _resolution = SubmissionCandidateResolution.None; bool isValidFile = false, isArchive = false, isFileSystem = false, isInArchive = false, isIgnoredButExported = false; bool shouldExplode = false; // @TODO Detect parents, etc. if (Parent != null) { if (Parent.IsArchive) { isInArchive = true; } } if (isInArchive == false) { // Is this a directory? if (FileTypes.Types.IsFileSystem(ReadPath)) { // Yes, it's a directory. isFileSystem = true; _containerPath = ReadPath; } else { // What about an Archive? if (FileTypes.Types.IsArchive(ReadPath, out _fileType)) { isArchive = true; } // Then it must be a file. // But before we start processing, we need to check to // see if it's a supported file. If not then it's ignored. else if (FileTypes.Types.TryGet(ReadPath, out _fileType)) { if (_fileType != FileTypes.Types.OctetStream) { isValidFile = true; } } } } else { // What if it's an archive in an archive?? - That's just crazy talk!! if (FileTypes.Types.IsArchiveInArchive(ReadPath, ArchiveInfo, out _fileType)) { isArchive = true; } else { if (FileTypes.Types.TryGetInArchive(ReadPath, ArchiveInfo, out _fileType)) { if (_fileType == FileTypes.Types.OctetStream) { isIgnoredButExported = true; } else { isValidFile = true; } } else { isIgnoredButExported = true; } } } // Is it an archive? // Mark it as an archive, and put up a flag so it can be exploded later. if (isArchive) { _resolution |= SubmissionCandidateResolution.Archive; shouldExplode = true; } // Is it a directory // Mark it as a directory, and put up a flag so it can be exploded later else if (isFileSystem) { _resolution |= SubmissionCandidateResolution.Directory; shouldExplode = true; } // Is it a valid file? // Then it's an submission else if (isValidFile) { _resolution |= SubmissionCandidateResolution.Submission; } if (isIgnoredButExported) { _resolution |= SubmissionCandidateResolution.IgnoreButExport; } // Apply the filters now bool filtered = false; // Hotfix for now, since .zip files are being applied as no-nos by the config if (isArchive == false) { filtered = candidates.ApplyFilter(this); } if (filtered) { _resolution |= SubmissionCandidateResolution.Filtered; } // If we need to explode and we aren't filtered, then explode! if (shouldExplode && filtered == false) { Explode(candidates, workingDirectory); } // Finally mark as resolved _resolution |= SubmissionCandidateResolution.Resolved; return(_resolution); }