|
1 | 1 | using CarCareTracker.Models; |
| 2 | +using CsvHelper; |
| 3 | +using System.Globalization; |
2 | 4 | using System.IO.Compression; |
3 | 5 |
|
4 | 6 | namespace CarCareTracker.Helper |
@@ -241,14 +243,41 @@ public string MakeAttachmentsExport(List<GenericReportModel> exportData) |
241 | 243 | if (!Directory.Exists(tempPath)) |
242 | 244 | Directory.CreateDirectory(tempPath); |
243 | 245 | int fileIndex = 0; |
| 246 | + List<AttachmentExportModel> urlAttachments = new List<AttachmentExportModel>(); |
244 | 247 | foreach (GenericReportModel reportModel in exportData) |
245 | 248 | { |
246 | 249 | foreach (UploadedFiles file in reportModel.Files) |
247 | 250 | { |
248 | 251 | var fileToCopy = GetFullFilePath(file.Location); |
249 | 252 | 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(); |
252 | 281 | } |
253 | 282 | } |
254 | 283 | var destFilePath = $"{tempPath}.zip"; |
|
0 commit comments