Traeger-GmbH / release-server

An server application for managing your own release artifacts via a REST API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

//If the directory already exists, delete the old content in there
if (Directory.Exists(path))
{
Logger.LogInformation("This path already exits! Old content will be deleted!");
var dirInfo = new DirectoryInfo(path);
dirInfo.Delete(true);
Logger.LogInformation("Old path successfully deleted!");
}
else
{
//Create the directory & delete the last directory hierarchy of the path
//(this is necessary, so that Directory.Move() below does not fail with "Directory already exists"
var dirInfo = Directory.CreateDirectory(path);
dirInfo.Delete();
Logger.LogInformation("The directory {0} was successfully created", dirInfo.Parent.FullName);
}
Directory.CreateDirectory(Path.Combine(ArtifactRoot, artifact.ProductInformation.ProductIdentifier));
//Move the extracted payload to the right directory
Directory.Move(tmpPath, path);

Proposal for improvement

  1. 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);
}
  1. 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;
    }
}