Rework mechanism for storing an uploaded artifact
f-porter opened this issue · comments
The implementation of method StoreArtifact()
of FsReleaseArtifactRepository
is not very comprehensible (e.g. the artifact directory is created and then deleted again).
See:
Proposal for improvement
- A new extension method
Move()
on DirectoryInfo:
public static void Move(this DirectoryInfo directory, string path, bool overwriteExisting = false)
{
var destination = new DirectoryInfo(path);
directory.Move(destination, overwriteExisting);
}
public static void Move(this DirectoryInfo directory, DirectoryInfo destination, bool overwriteExisting = false)
{
destination.Refresh();
if (destination.Exists)
{
if (overwriteExisting)
{
destination.Delete(true);
}
else
{
throw new IOException($"Could not rename directory: New path {destination.FullName} already exists.");
}
}
destination.CreateParent();
Directory.Move(directory.FullName, destination.FullName);
}
- Replace the code in
FsReleaseArtifactRepository.StoreArtifact()
:
public void StoreArtifact(ReleaseArtifactModel artifact)
{
var artifactPath = GenerateArtifactPath(
artifact.ProductInformation.ProductIdentifier,
artifact.ProductInformation.Os,
artifact.ProductInformation.HwArchitecture,
artifact.ProductInformation.Version.ToString());
var tmpDir = new DirectoryInfo(GenerateTemporaryPath());
try
{
//Create the temporary directory
if (!tmpDir.Exists)
tmpDir.Create();
//Extract the payload to the temporary directory
artifact.Payload.ExtractToDirectory(tmpDir.ToString());
Logger.LogDebug("The Artifact was successfully unpacked & stored to the temp directory");
var artifactDirectory = new DirectoryInfo(artifactPath);
if (artifactDirectory.Exists)
{
Logger.LogDebug("Artifact already exists and will be replaced.");
}
tmpDir.Move(artifactDirectory, true);
Logger.LogInformation("The Artifact was successfully stored");
//Cleanup the tmp directory
tmpDir.Parent.Delete(true);
}
catch (Exception e)
{
Logger.LogCritical(e.Message);
throw;
}
}