Skip to content

Commit

Permalink
Projektdateien hinzufügen.
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlKeu00 committed Apr 16, 2020
1 parent 1d6017c commit 8ed5636
Show file tree
Hide file tree
Showing 9 changed files with 730 additions and 0 deletions.
25 changes: 25 additions & 0 deletions MyStuff2Docx.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30002.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyStuff2Docx", "MyStuff2Docx\MyStuff2Docx.csproj", "{1E660EB5-519D-4959-95A6-603CEC277CF6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1E660EB5-519D-4959-95A6-603CEC277CF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E660EB5-519D-4959-95A6-603CEC277CF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E660EB5-519D-4959-95A6-603CEC277CF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E660EB5-519D-4959-95A6-603CEC277CF6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E82253D9-72CD-44A7-9C13-01F467709E37}
EndGlobalSection
EndGlobal
124 changes: 124 additions & 0 deletions MyStuff2Docx/Models/MyStuffCategory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using CsvHelper;
using DocumentFormat.OpenXml.Packaging;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;

namespace MyStuff2Docx.Models {
class MyStuffCategory : IDisposable {
private ZipArchive categoryZipArchive;
public ZipArchive CategoryZipArchive => categoryZipArchive ??= ZipFile.OpenRead(LocalZipFilePath);


public string TempImagesPath { get; set; }
public string LocalZipFilePath { get; set; }
public string ZipFileName { get; set; }
public string Name { get; set; }
public bool Selected { get; set; } = false;


private List<MyStuffItemInfo> itemInfos;
public List<MyStuffItemInfo> ItemInfos => itemInfos ??= getItemInfos();

private List<MyStuffImage> images;
public List<MyStuffImage> Images => images ??= getImages();



private List<MyStuffItemInfo> getItemInfos() {
var result = new List<MyStuffItemInfo>();

var infoCsvFile = CategoryZipArchive.Entries.SingleOrDefault(e => e.Name.Equals($"{Name}.csv"));
if (infoCsvFile != null) {
using (var rawCsvReader = new StreamReader(infoCsvFile.Open())) {
using (var csv = new CsvReader(rawCsvReader, CultureInfo.InvariantCulture)) {
csv.Read();
csv.ReadHeader();
while (csv.Read()) {
var newItemInfo = new MyStuffItemInfo() {
Id = csv.GetField<string>("item id"),
ItemLocation = csv.GetField<string>("item location"),
Images = csv.GetField<string>("item images").Split("|").Where(x => !string.IsNullOrEmpty(x)).ToArray(),
Attachments = csv.GetField<string>("item attachments").Split("|").Where(x => !string.IsNullOrEmpty(x)).ToArray()
};

try {
newItemInfo.ItemUpdated = DateTime.ParseExact(csv.GetField<string>("item updated"), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
}
catch (Exception) { }
try {
newItemInfo.ItemCreated = DateTime.ParseExact(csv.GetField<string>("item created"), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
}
catch (Exception) { }

newItemInfo.AdditionalProperties ??= new Dictionary<string, string> { };
foreach (var header in csv.Context.HeaderRecord) {
newItemInfo.AdditionalProperties.TryAdd(header, csv.TryGetField(header, out string value) ? value : string.Empty);
}

result.Add(newItemInfo);
}
}
}
}

return result;
}
private List<MyStuffImage> getImages() {
var result = new List<MyStuffImage>();

foreach (var compressedImage in CategoryZipArchive.Entries) {
var imageInfo = new MyStuffImage();

switch (Path.GetExtension(compressedImage.Name)) {
case ".jpg":
case ".jpeg":
imageInfo.ImageType = ImagePartType.Jpeg;
break;
case ".png":
imageInfo.ImageType = ImagePartType.Png;
break;
case ".bmp":
imageInfo.ImageType = ImagePartType.Bmp;
break;
case ".gif":
imageInfo.ImageType = ImagePartType.Gif;
break;
case ".tiff":
imageInfo.ImageType = ImagePartType.Tiff;
break;
default:
continue;
}

imageInfo.ImageFileName = compressedImage.Name;
imageInfo.PathInCategory = compressedImage.FullName;
imageInfo.ImageId = Path.GetFileNameWithoutExtension(compressedImage.Name);
imageInfo.ItemId = compressedImage.FullName.Split('\\', '/').FirstOrDefault();
imageInfo.TempImagePath = TempImagesPath + "Temp_Image_" + Guid.NewGuid() + ".tmp";

using (var compressedImageStream = compressedImage.Open()) {
using (var tempImageStream = File.Open(imageInfo.TempImagePath, FileMode.OpenOrCreate)) {
compressedImageStream.CopyTo(tempImageStream);
}
}

result.Add(imageInfo);
}


return result;
}


public void Dispose() {
if (categoryZipArchive != null) {
categoryZipArchive.Dispose();
categoryZipArchive = null;
}
}
}
}
13 changes: 13 additions & 0 deletions MyStuff2Docx/Models/MyStuffImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using DocumentFormat.OpenXml.Packaging;

namespace MyStuff2Docx.Models {
class MyStuffImage {
public string PathInCategory { get; set; }
public string ImageFileName { get; set; }
public string ItemId { get; set; }
public string ImageId { get; set; }
public ImagePartType ImageType { get; set; }

public string TempImagePath { get; set; }
}
}
28 changes: 28 additions & 0 deletions MyStuff2Docx/Models/MyStuffItemInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace MyStuff2Docx.Models {
class MyStuffItemInfo {
public string Id { get; set; }
public string ItemLocation { get; set; }

public Dictionary<string, string> AdditionalProperties { get; set; }

public string[] Images { get; set; }
public string[] Attachments { get; set; }
public DateTime ItemUpdated { get; set; }
public DateTime ItemCreated { get; set; }

public bool AdditionalPropertyFilter(KeyValuePair<string, string> property) {
var conditions = new List<bool> {
!property.Key.Equals("item id"),
!property.Key.Equals("item barcode"),
!property.Key.Equals("item images"),
!property.Key.Equals("item attachments")
};

return conditions.All(c => c);
}
}
}
14 changes: 14 additions & 0 deletions MyStuff2Docx/MyStuff2Docx.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CsvHelper" Version="15.0.4" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.10.1" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
</ItemGroup>

</Project>
50 changes: 50 additions & 0 deletions MyStuff2Docx/MyStuffHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using MyStuff2Docx.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;

namespace MyStuff2Docx {
class MyStuffHandler : IDisposable {
public string BaseZipArchiveTempPath { get; private set; }
public string TempImagesPath { get; private set; }
public string TempDocxPath { get; private set; }


public List<MyStuffCategory> Categories { get; private set; }


public MyStuffHandler(string path) {
BaseZipArchiveTempPath = Path.GetTempPath() + "MyStuff2Docx_ZipFiles_" + Guid.NewGuid() + Path.DirectorySeparatorChar;
TempImagesPath = Path.GetTempPath() + "MyStuff2Docx_Images_" + Guid.NewGuid() + Path.DirectorySeparatorChar;
TempDocxPath = Path.GetTempPath() + "MyStuff2Docx_DocxFiles_" + Guid.NewGuid() + Path.DirectorySeparatorChar;
Directory.CreateDirectory(BaseZipArchiveTempPath);
Directory.CreateDirectory(TempImagesPath);
Directory.CreateDirectory(TempDocxPath);

ZipFile.ExtractToDirectory(path, BaseZipArchiveTempPath);

Categories ??= new List<MyStuffCategory>();
foreach (var compressedCategory in Directory.GetFiles(BaseZipArchiveTempPath, "*.zip")) {
var newCategory = new MyStuffCategory() {
ZipFileName = Path.GetFileName(compressedCategory),
Name = Path.GetFileNameWithoutExtension(compressedCategory),
TempImagesPath = TempImagesPath,
LocalZipFilePath = compressedCategory
};

Categories.Add(newCategory);
}
}

public void Dispose() {
foreach (var category in Categories ?? new List<MyStuffCategory> { }) {
category.Dispose();
}

Directory.Delete(BaseZipArchiveTempPath, true);
Directory.Delete(TempImagesPath, true);
Directory.Delete(TempDocxPath, true);
}
}
}
58 changes: 58 additions & 0 deletions MyStuff2Docx/ProcessingMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Diagnostics;

namespace MyStuff2Docx {
class ProcessingMessage : IDisposable {
private const string PROCESSING_MESSAGE = "Processing...";
private const string DONE_MESSAGE = "Done";
private const string ERROR_MESSAGE = "Error!";
private const ConsoleColor PROCESSING_COLOR = ConsoleColor.Yellow;
private const ConsoleColor DONE_COLOR = ConsoleColor.Green;
private const ConsoleColor ERROR_COLOR = ConsoleColor.Red;

private int CursorXPosition;
private int CursorYPosition;

public bool HasError { get; set; } = false;

public ProcessingMessage(string text) {
lock (Program._CONSOLE_WRITE_LOCK) {
var currentConsoleColor = Console.ForegroundColor;

Console.Write($"{text} --> ");
Console.ForegroundColor = PROCESSING_COLOR;
Console.Write($"{PROCESSING_MESSAGE}");
Console.ForegroundColor = currentConsoleColor;

CursorXPosition = Console.CursorLeft;
CursorYPosition = Console.CursorTop;
Console.WriteLine();
}
}

public void Dispose() {
lock (Program._CONSOLE_WRITE_LOCK) {
var currentXPosition = Console.CursorLeft;
var currentYPosition = Console.CursorTop;
var currentConsoleColor = Console.ForegroundColor;

try {
Console.SetCursorPosition(CursorXPosition - PROCESSING_MESSAGE.Length, CursorYPosition);
}
finally {
if (HasError) {
Console.ForegroundColor = ERROR_COLOR;
Console.Write(ERROR_MESSAGE.PadRight(PROCESSING_MESSAGE.Length));
}
else {
Console.ForegroundColor = DONE_COLOR;
Console.Write(DONE_MESSAGE.PadRight(PROCESSING_MESSAGE.Length));
}

Console.SetCursorPosition(currentXPosition, currentYPosition);
Console.ForegroundColor = currentConsoleColor;
}
}
}
}
}
Loading

0 comments on commit 8ed5636

Please sign in to comment.