Skip to content

Commit 10b420c

Browse files
authored
Merge pull request #1053 from hargata/Hargata/1050
URL Attachments now export separately.
2 parents e490117 + 5f6f713 commit 10b420c

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

Helper/FileHelper.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using CarCareTracker.Models;
2+
using CsvHelper;
3+
using System.Globalization;
24
using System.IO.Compression;
35

46
namespace CarCareTracker.Helper
@@ -241,14 +243,41 @@ public string MakeAttachmentsExport(List<GenericReportModel> exportData)
241243
if (!Directory.Exists(tempPath))
242244
Directory.CreateDirectory(tempPath);
243245
int fileIndex = 0;
246+
List<AttachmentExportModel> urlAttachments = new List<AttachmentExportModel>();
244247
foreach (GenericReportModel reportModel in exportData)
245248
{
246249
foreach (UploadedFiles file in reportModel.Files)
247250
{
248251
var fileToCopy = GetFullFilePath(file.Location);
249252
var destFileName = $"{tempPath}/{fileIndex}_{reportModel.DataType}_{reportModel.Date.ToString("yyyy-MM-dd")}_{file.Name}{Path.GetExtension(file.Location)}";
250-
File.Copy(fileToCopy, destFileName);
251-
fileIndex++;
253+
if (File.Exists(fileToCopy))
254+
{
255+
File.Copy(fileToCopy, destFileName);
256+
fileIndex++;
257+
} else
258+
{
259+
//file not found, must be a URL
260+
urlAttachments.Add(new AttachmentExportModel {
261+
DataType = reportModel.DataType.ToString(),
262+
Date = reportModel.Date.ToString("yyyy-MM-dd"),
263+
Name = file.Name,
264+
Location = file.Location
265+
});
266+
}
267+
}
268+
}
269+
if (urlAttachments.Any())
270+
{
271+
//write csv file detailing all urls.
272+
var destFileName = $"{tempPath}/link_attachments.csv";
273+
using (var writer = new StreamWriter(destFileName))
274+
{
275+
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
276+
{
277+
//custom writer
278+
StaticHelper.WriteAttachmentExportModel(csv, urlAttachments);
279+
}
280+
writer.Dispose();
252281
}
253282
}
254283
var destFilePath = $"{tempPath}.zip";

Helper/StaticHelper.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,23 @@ public static void WritePlanRecordExportModel(CsvWriter _csv, IEnumerable<PlanRe
709709
_csv.NextRecord();
710710
}
711711
}
712+
public static void WriteAttachmentExportModel(CsvWriter _csv, IEnumerable<AttachmentExportModel> genericRecords)
713+
{
714+
//write headers
715+
_csv.WriteField(nameof(AttachmentExportModel.DataType));
716+
_csv.WriteField(nameof(AttachmentExportModel.Date));
717+
_csv.WriteField(nameof(AttachmentExportModel.Name));
718+
_csv.WriteField(nameof(AttachmentExportModel.Location));
719+
_csv.NextRecord();
720+
foreach (AttachmentExportModel genericRecord in genericRecords)
721+
{
722+
_csv.WriteField(genericRecord.DataType);
723+
_csv.WriteField(genericRecord.Date);
724+
_csv.WriteField(genericRecord.Name);
725+
_csv.WriteField(genericRecord.Location);
726+
_csv.NextRecord();
727+
}
728+
}
712729
public static string HideZeroCost(string input, bool hideZero, string decorations = "")
713730
{
714731
if (input == 0M.ToString("C2") && hideZero)

Models/Shared/ImportModel.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,11 @@ public class UserExportModel
176176
[JsonConverter(typeof(FromBoolOptional))]
177177
public string IsRoot { get; set; }
178178
}
179+
public class AttachmentExportModel
180+
{
181+
public string DataType { get; set; }
182+
public string Date { get; set; }
183+
public string Name { get; set; }
184+
public string Location { get; set; }
185+
}
179186
}

0 commit comments

Comments
 (0)