164 lines
5.3 KiB
C#
164 lines
5.3 KiB
C#
using BS.SharedLauncher.Enums;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml;
|
|
using System.Xml.Linq;
|
|
|
|
namespace BS.SharedLauncher.Update
|
|
{
|
|
public class PlanerRoot
|
|
{
|
|
public List<PlanerFile> Files { get; set; }
|
|
public DateTime Created { get; set; }
|
|
|
|
public string DefaultVersion { get; set; }
|
|
public string TenantVersion { get; set; }
|
|
|
|
//public List<PlanerDirectory> Directories { get; set; }
|
|
//public List<PlanerFile> Files { get; set; }
|
|
|
|
public PlanerRoot()
|
|
{
|
|
Created = DateTime.Now;
|
|
}
|
|
|
|
public string CurrentVersion => TenantVersion ?? DefaultVersion;
|
|
|
|
public PlanerFile FindPlanerFile(PlanerFile file)
|
|
{
|
|
return Files.Find(x => x.RelativePath == file.RelativePath);
|
|
}
|
|
|
|
public void InitPlanerFiles(List<PlanerFile> files)
|
|
{
|
|
Files = new List<PlanerFile>();
|
|
Files.AddRange(files);
|
|
}
|
|
|
|
private void UpdatePlanerFile(PlanerFile newFile)
|
|
{
|
|
var oldFile = FindPlanerFile(newFile);
|
|
|
|
oldFile.AbsolutePath = newFile.AbsolutePath;
|
|
oldFile.Source = newFile.Source;
|
|
}
|
|
public void UpdatePlanerFiles(List<PlanerFile> files) => files.ForEach(x => UpdatePlanerFile(x));
|
|
|
|
public byte[] GetData()
|
|
{
|
|
byte[] res = null;
|
|
|
|
using (var memoryStream = new MemoryStream())
|
|
{
|
|
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
|
|
{
|
|
var demoFile = archive.CreateEntry("foo.txt");
|
|
|
|
using (var entryStream = demoFile.Open())
|
|
using (var streamWriter = new StreamWriter(entryStream))
|
|
{
|
|
streamWriter.Write("Bar!");
|
|
}
|
|
}
|
|
|
|
res = memoryStream.ToArray();
|
|
|
|
// memoryStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
//using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
|
|
//{
|
|
// memoryStream.Seek(0, SeekOrigin.Begin);
|
|
// memoryStream.CopyTo(fileStream);
|
|
//}
|
|
}
|
|
|
|
return res;
|
|
|
|
//using (var compressedFileStream = new MemoryStream())
|
|
//{
|
|
// //Create an archive and store the stream in memory.
|
|
// using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Create, false))
|
|
// {
|
|
// foreach (var caseAttachmentModel in caseAttachmentModels)
|
|
// {
|
|
// //Create a zip entry for each attachment
|
|
// var zipEntry = zipArchive.CreateEntry(caseAttachmentModel.Name);
|
|
|
|
// //Get the stream of the attachment
|
|
// using (var originalFileStream = new MemoryStream(caseAttachmentModel.Body))
|
|
// using (var zipEntryStream = zipEntry.Open())
|
|
// {
|
|
// //Copy the attachment stream to the zip entry stream
|
|
// originalFileStream.CopyTo(zipEntryStream);
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// return new FileContentResult(compressedFileStream.ToArray(), "application/zip") { FileDownloadName = "Filename.zip" };
|
|
//}
|
|
}
|
|
|
|
public string GetVersionXml(string tenant)
|
|
{
|
|
XElement versions = new XElement("example");
|
|
|
|
XDocument root = new XDocument(
|
|
new XElement("bewopackage",
|
|
GetConfigElement(tenant),
|
|
GetFilesElement()
|
|
)
|
|
);
|
|
|
|
root.Save("D:\\document.xml");
|
|
|
|
//XDocument doc = new XDocument(
|
|
// new XElement("body",
|
|
// new XElement("level1",
|
|
// new XElement("level2", "text"),
|
|
// new XElement("level2", "other text"))));
|
|
//doc.Save("D:\\document.xml");
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
private XElement GetConfigElement(string tenant)
|
|
{
|
|
XElement element = new XElement("config");
|
|
|
|
element.Add(new XElement("tenant", tenant));
|
|
element.Add(new XElement("dversion", DefaultVersion));
|
|
element.Add(new XElement("tversion", TenantVersion));
|
|
element.Add(new XElement("created", Created.ToString()));
|
|
|
|
return element;
|
|
}
|
|
|
|
private XElement GetFilesElement()
|
|
{
|
|
XElement element = new XElement("files");
|
|
|
|
Files.ForEach(x => element.Add(GetFileElement(x)));
|
|
|
|
return element;
|
|
}
|
|
|
|
private XElement GetFileElement(PlanerFile planerFile)
|
|
{
|
|
XElement element = new XElement("file");
|
|
|
|
element.Add(new XElement("name", planerFile.Name));
|
|
element.Add(new XElement("relative", planerFile.RelativePath));
|
|
//element.Add(new XElement("extension", planerFile.Extension));
|
|
element.Add(new XElement("source", planerFile.Source));
|
|
|
|
return element;
|
|
}
|
|
}
|
|
}
|