/// <summary> /// Creates an identifier in the set and returns it for the given /// raw id /// </summary> /// <param name="identifier">The identifier provided by the client</param> /// <param name="id">The id set to add to</param> /// <returns>The created id for the provided identifier</returns> private Guid _createIdentifier(object identifier, PersistedJobIdentifiers id) { if (identifier == null) { return(id.CreateIdentifier()); } else { return(id.CreateIdentifier(identifier)); } }
/// <summary> /// Gets the generated identifier for the input. /// </summary> /// <param name="jobID">The identifier of the job the input was saved /// against.</param> /// <param name="originalIdentifier">The original identifier given to the /// input.</param> /// <returns>The unique identifier for the input provided by the /// persister.</returns> public Guid GetPersistedIdentifier(Guid jobID, object originalIdentifier) { PersistedJobIdentifiers id = _getOrCreateIDSet(jobID); Guid?theID = id.GetIdentifier(originalIdentifier); if (theID.HasValue == false) { throw new ArgumentException("Result not persisted with identifier"); } return(theID.Value); }
/// <summary> /// Gets or creates the id set object for the job /// </summary> /// <param name="jobGuid">The job to retrieve the set for</param> /// <returns>The job ID set object</returns> private PersistedJobIdentifiers _getOrCreateIDSet(Guid jobGuid) { PersistedJobIdentifiers id = _identifiers.FirstOrDefault(x => x.JobID == jobGuid); if (id == null) { id = new PersistedJobIdentifiers(jobGuid); _identifiers.Add(id); } return(id); }
/// <summary> /// Persits the output of a job. /// </summary> /// <param name="jobID">The unique identifier of the job to save /// the image against.</param> /// <param name="output">The <see cref="Image"/> generated from a /// complete job.</param> /// <param name="identifier">The identifier for the input provided /// by the client.</param> public void Persist(Guid jobID, Image output, object identifier) { PersistedJobIdentifiers id = _getOrCreateIDSet(jobID); Guid inputID = _createIdentifier(identifier, id); string idAsString = inputID.ToString(); string fullPath = _resolvePath(idAsString, jobID); using (Stream stream = File.Create(fullPath)) { output.Save(stream, ImageFormat.Png); } }
/// <summary> /// Attempts to create a result for the given file. /// </summary> /// <param name="jobID">The id of the job the result came from</param> /// <param name="fileName">The path to the file</param> /// <returns>A PersistedResult instance, or null if the file /// cannot be converted.</returns> private PersistedResult _createResult(Guid jobID, string fileName) { try { Image img = _loadImage(fileName); string id = Path.GetFileNameWithoutExtension(fileName); Guid idAsGuid = Guid.Parse(id); PersistedJobIdentifiers ids = _getOrCreateIDSet(jobID); object theID = ids.GetOriginalIdentifier(idAsGuid); return(new PersistedResult(img, theID)); } catch { return(null); } }
/// <summary> /// Deletes all the results from a particular job from the storage. /// </summary> /// <param name="jobID">The unique identifier of the job to delete /// the results for.</param> /// <returns><c>true</c> if the results from the job were deleted /// successfully; <c>false</c> otherwise.</returns> public bool Delete(Guid jobID) { string dir = string.Format(@"{0}/{{{1}}}", TargetDirectory, jobID); if (Directory.Exists(dir)) { Directory.Delete(dir, true); PersistedJobIdentifiers id = _getOrCreateIDSet(jobID); _identifiers.Remove(id); return(true); } else { return(false); } }
/// <summary> /// Deletes a result from a particular job with the associated identifier /// from the storage. /// </summary> /// <param name="jobID">The unique identifier of the job to delete /// the results for.</param> /// <param name="identifier">The identifier given to the input to be /// deleted.</param> /// <returns><c>true</c> if the result from the job was deleted /// successfully; <c>false</c> otherwise.</returns> public bool Delete(Guid jobID, object identifier) { PersistedJobIdentifiers id = _getOrCreateIDSet(jobID); Guid?theID = id.GetIdentifier(identifier); if (theID.HasValue == false) { return(false); } string path = string.Format(@"{0}/{{{1}}}/{2}.png", TargetDirectory, jobID, theID.Value.ToString()); if (File.Exists(path)) { File.Delete(path); id.RemoveOriginalIdentifier(identifier); return(true); } else { return(false); } }