2019-12-14 20:11:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-03-26 23:02:15 +00:00
|
|
|
|
using AlphaPath = Alphaleonis.Win32.Filesystem.Path;
|
2019-12-14 20:11:39 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
|
|
|
|
{
|
2020-05-28 02:43:57 +00:00
|
|
|
|
public class TempFile : IAsyncDisposable
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
|
|
|
|
public FileInfo File { get; private set; }
|
2020-03-26 23:02:15 +00:00
|
|
|
|
public AbsolutePath Path => (AbsolutePath)File.FullName;
|
2019-12-14 20:11:39 +00:00
|
|
|
|
public bool DeleteAfter = true;
|
|
|
|
|
|
|
|
|
|
public TempFile(bool deleteAfter = true, bool createFolder = true)
|
2020-04-03 03:57:59 +00:00
|
|
|
|
: this(new FileInfo((string)GetTempFilePath()))
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2020-10-10 03:02:58 +00:00
|
|
|
|
|
|
|
|
|
public TempFile(AbsolutePath path, bool deleteAfter = true, bool createFolder = true)
|
|
|
|
|
: this(new FileInfo((string)path))
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-12-14 20:11:39 +00:00
|
|
|
|
|
2020-04-03 03:57:59 +00:00
|
|
|
|
private static AbsolutePath GetTempFilePath()
|
|
|
|
|
{
|
2020-04-03 20:19:10 +00:00
|
|
|
|
var path = (@"temp\" + Guid.NewGuid()).RelativeTo(AbsolutePath.EntryPoint).WithExtension(Consts.TempExtension);
|
2020-04-03 03:57:59 +00:00
|
|
|
|
path.Parent.CreateDirectory();
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-14 20:11:39 +00:00
|
|
|
|
public TempFile(FileInfo file, bool deleteAfter = true, bool createFolder = true)
|
|
|
|
|
{
|
|
|
|
|
this.File = file;
|
2020-12-31 23:39:18 +00:00
|
|
|
|
if (createFolder && file.Directory != null && !file.Directory!.Exists)
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
|
|
|
|
file.Directory.Create();
|
|
|
|
|
}
|
|
|
|
|
this.DeleteAfter = deleteAfter;
|
|
|
|
|
}
|
2020-11-02 04:06:18 +00:00
|
|
|
|
|
|
|
|
|
public TempFile(Extension ext)
|
|
|
|
|
:this(new FileInfo((string)GetTempFilePath().WithExtension(ext)))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 02:43:57 +00:00
|
|
|
|
public async ValueTask DisposeAsync()
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
2020-08-12 22:23:02 +00:00
|
|
|
|
if (DeleteAfter && Path.Exists)
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
2020-05-28 02:43:57 +00:00
|
|
|
|
await Path.DeleteAsync();
|
2019-12-14 20:11:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|