Skip to content

Commit 4260ab7

Browse files
committed
#445 update filenames for download
1 parent 3592b3d commit 4260ab7

File tree

7 files changed

+132
-32
lines changed

7 files changed

+132
-32
lines changed

Components/IO/BExIS.IO.Tests/IOHelperTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ public void OneTimeTearDown()
3131
public void GetDynamicStorePathValuesTest(
3232
[Range(1, 2)] long datasetId, [Range(1, 2)] long datasetVersionOrderNr)
3333
{
34-
string path = IoHelper.GetDynamicStorePath(datasetId, datasetVersionOrderNr, "title", ".txt");
34+
string path = IOHelper.GetDynamicStorePath(datasetId, datasetVersionOrderNr, "title", ".txt");
3535

3636
path.Should().NotBeNull("Because path is not null.");
3737
}
3838

3939
[TestCase(1, 2, "test", ".txt", ExpectedResult = @"Datasets\1\DatasetVersions\1_2_test.txt")]
4040
public string GetDynamicStorePathResultTest(long datasetId, long datasetVersionOrderNr, string title, string extention)
4141
{
42-
return IoHelper.GetDynamicStorePath(datasetId, datasetVersionOrderNr, title, extention);
42+
return IOHelper.GetDynamicStorePath(datasetId, datasetVersionOrderNr, title, extention);
4343
}
4444

4545
[TestCase(-1, 1, "title", ".txt")]
@@ -48,7 +48,7 @@ public string GetDynamicStorePathResultTest(long datasetId, long datasetVersionO
4848
[TestCase(1, 1, "title", "")]
4949
public void GetDynamicStorePathWidthExceptionTest(long datasetId, long datasetVersionOrderNr, string title, string extention)
5050
{
51-
Assert.Throws<Exception>(() => IoHelper.GetDynamicStorePath(datasetId, datasetVersionOrderNr, title, extention));
51+
Assert.Throws<Exception>(() => IOHelper.GetDynamicStorePath(datasetId, datasetVersionOrderNr, title, extention));
5252
}
5353
}
5454
}

Components/IO/BExIS.IO/BExIS.IO.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@
6767
<Analyzer Include="..\..\..\packages\Microsoft.CodeAnalysis.NetAnalyzers.8.0.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll" />
6868
<Analyzer Include="..\..\..\packages\Microsoft.CodeAnalysis.NetAnalyzers.8.0.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.NetAnalyzers.dll" />
6969
</ItemGroup>
70+
<ItemGroup>
71+
<ProjectReference Include="..\..\Utils\BExIS.Utils.Config\BExIS.Utils.Config.csproj">
72+
<Project>{6EAD7D02-02F7-42FF-85E4-90BB892D3846}</Project>
73+
<Name>BExIS.Utils.Config</Name>
74+
</ProjectReference>
75+
</ItemGroup>
7076
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7177
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
7278
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

Components/IO/BExIS.Io.Transform.Output/OutputDatasetManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static GFBIOPangaeaFormularObject GetGFBIOPangaeaFormularObject(long data
7474

7575
public static string GetDynamicDatasetStorePath(long datasetId, long datasetVersionOrderNr, string title, string extention)
7676
{
77-
return IoHelper.GetDynamicStorePath(datasetId, datasetVersionOrderNr, title, extention);
77+
return IOHelper.GetDynamicStorePath(datasetId, datasetVersionOrderNr, title, extention);
7878
}
7979

8080
public static XmlDocument GenerateManifest(long datasetId, long versionId)

Components/IO/BExIS.Io/IoHelper.cs

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System;
1+
using BExIS.Utils.Config;
2+
using System;
23
using System.IO;
34

45
namespace BExIS.IO
56
{
6-
public class IoHelper
7+
public class IOHelper
78
{
89
public static string GetDynamicStorePath(long datasetId, long datasetVersionOrderNr, string title, string extention)
910
{
@@ -14,9 +15,86 @@ public static string GetDynamicStorePath(long datasetId, long datasetVersionOrde
1415
if (extention.IndexOf('.') == -1) throw new Exception("Extention should start with '.' .");
1516

1617
string storePath = Path.Combine("Datasets", datasetId.ToString(), "DatasetVersions");
18+
string fileName = datasetId + "_" + datasetVersionOrderNr + "_" + title + extention;//GetFileName(FileType.None, datasetId, (int)datasetVersionOrderNr, 0, title);
1719

18-
return Path.Combine(storePath, datasetId + "_" + datasetVersionOrderNr + "_" + title + extention);
20+
return Path.Combine(storePath, fileName);
1921
}
22+
23+
public static string GetDynamicStoreFilePath(long datasetId, long datasetVersionOrderNr, string name, string extention)
24+
{
25+
if (datasetId < 1) throw new Exception("Dataset id can not be less then 1.");
26+
if (datasetVersionOrderNr < 1) throw new Exception("Dataset version number can not be less then 1.");
27+
if (string.IsNullOrEmpty(name)) throw new Exception("Title should not be Empty.");
28+
if (string.IsNullOrEmpty(extention)) throw new Exception("Extention should not be Empty.");
29+
if (extention.IndexOf('.') == -1) throw new Exception("Extention should start with '.' .");
30+
31+
string storePath = Path.Combine("Datasets", datasetId.ToString(), "DatasetVersions");
32+
string fileName = GetFileName(FileType.None, datasetId, (int)datasetVersionOrderNr, 0, name);
33+
34+
return Path.Combine(storePath, fileName + extention);
35+
}
36+
37+
public static string GetFileName(FileType type, long datasetId, int versionNr, long datastructureId, string title = "")
38+
{
39+
string appName = GeneralSettings.ApplicationName;
40+
if (string.IsNullOrEmpty(appName)) appName = "BEXIS2";
41+
42+
string downloadName = "no title available";
43+
string downloadDate = DateTime.Now.ToString("yyyyMMdd");
44+
45+
string downloadTitle = title.Replace(" ", "");
46+
47+
switch (type)
48+
{
49+
case FileType.Metadata:
50+
// filename should contain: application name, dataset ID, and version ID
51+
downloadName = string.Format("{0}_{1}_v{2}_metadata", appName, datasetId, versionNr);
52+
break;
53+
case FileType.MetadataExport:
54+
// filename should contain: application name, dataset ID, and version ID
55+
downloadName = string.Format("{0}_{1}_v{2}_metadata_{3}", appName, datasetId, versionNr, title);
56+
break;
57+
case FileType.DataStructure:
58+
downloadName = string.Format("{0}_{1}_datastructure_{2}", appName, datasetId, datastructureId);
59+
break;
60+
case FileType.PrimaryData:
61+
downloadName = string.Format("{0}_{1}_v{2}_data", appName, datasetId, versionNr);
62+
63+
break;
64+
case FileType.PrimaryDataFiles:
65+
downloadName = string.Format("{0}_{1}_v{2}_data_{3}", appName, datasetId, versionNr, title);
66+
break;
67+
case FileType.Attachments:
68+
downloadName = string.Format("{0}_{1}_v{2}_attachment_{3}", appName, datasetId, versionNr, title);
69+
break;
70+
case FileType.Bundle:
71+
downloadName = string.Format("{0}_{1}_v{2}_{3}_{4}", appName, datasetId, versionNr, downloadTitle, downloadDate);
72+
break;
73+
case FileType.Manifest:
74+
downloadName = string.Format("{0}_{1}_v{2}_general-metadata", appName, datasetId, versionNr);
75+
break;
76+
default:
77+
downloadName = string.Format("{0}_{1}_v{2}_{3}", appName, datasetId, versionNr, title);
78+
break;
79+
}
80+
81+
82+
return downloadName;
83+
}
84+
85+
}
86+
87+
public enum FileType
88+
{
89+
Metadata,
90+
MetadataExport,
91+
PrimaryData,
92+
PrimaryDataFiles,
93+
DataStructure,
94+
Attachments,
95+
Bundle,
96+
Manifest,
97+
None
2098
}
2199

22100
public enum DecimalCharacter

Components/XML/BExIS.Xml.Helpers/BExIS.Xml.Helpers.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
</ItemGroup>
7272
<ItemGroup>
7373
<Compile Include="DatasetStore.cs" />
74-
<Compile Include="IOHelper.cs" />
7574
<Compile Include="Mapping\XmlMapperManager.cs" />
7675
<Compile Include="Mapping\XmlSchemaManager.cs" />
7776
<Compile Include="SampleStore.cs" />

Components/XML/BExIS.Xml.Helpers/IOHelper.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

Console/BExIS.Web.Shell/Areas/DIM/BExIS.Modules.Dim.UI/Controllers/ExportController.cs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using System.IO;
2929
using System.IO.Compression;
3030
using System.Linq;
31+
using System.Security.Cryptography;
3132
using System.Text;
3233
using System.Web.Mvc;
3334
using System.Web.Routing;
@@ -203,13 +204,14 @@ public ActionResult GenerateZip(long id, long versionid, string format)
203204
long dataStructureId = 0;
204205
int datasetVersionNumber = dm.GetDatasetVersionNr(datasetVersionId);
205206
DatasetVersion datasetVersion = datasetManager.GetDatasetVersion(datasetVersionId);
207+
string title = "";
206208

207209
#region Metadata
208210

209211
//metadata as XML
210212
XmlDocument document = OutputMetadataManager.GetConvertedMetadata(id, TransmissionType.mappingFileExport, datasetVersion.Dataset.MetadataStructure.Name);
211213

212-
//generate metadata as HTML and store the file locally
214+
//generate data structure as html
213215
generateMetadataAsHtml(datasetVersion);
214216

215217
#endregion
@@ -222,7 +224,7 @@ public ActionResult GenerateZip(long id, long versionid, string format)
222224
OutputDataManager odm = new OutputDataManager();
223225

224226
//check wheter title is empty or not
225-
string title = String.IsNullOrEmpty(datasetVersion.Title) ? "no title available" : datasetVersion.Title;
227+
title = String.IsNullOrEmpty(datasetVersion.Title) ? "no title available" : datasetVersion.Title;
226228

227229
switch (format)
228230
{
@@ -260,16 +262,15 @@ public ActionResult GenerateZip(long id, long versionid, string format)
260262

261263
AsciiWriter.AllTextToFile(datastructureFilePath, json);
262264

263-
//generate data structure as html
264265
generateDataStructureHtml(datasetVersion);
265266
}
266267

267268
#endregion
268269

269270
#region zip file
270271

271-
string zipName = publishingManager.GetZipFileName(id, datasetVersionNumber);
272-
string zipPath = Path.Combine(publishingManager.GetDirectoryPath(id, brokerName), zipName);
272+
string zipName = IOHelper.GetFileName(FileType.Bundle, id, datasetVersionNumber, dataStructureId, title); //publishingManager.GetZipFileName(id, datasetVersionNumber);
273+
string zipPath = Path.Combine(publishingManager.GetDirectoryPath(id, brokerName), zipName+".zip");
273274
FileHelper.CreateDicrectoriesIfNotExist(Path.GetDirectoryName(zipPath));
274275

275276
using (var zipFileStream = new FileStream(zipPath, FileMode.Create))
@@ -283,13 +284,33 @@ public ActionResult GenerateZip(long id, long versionid, string format)
283284
{
284285
string path = Path.Combine(AppConfiguration.DataPath, cd.URI);
285286
string name = cd.URI.Split('\\').Last();
287+
string ext = Path.GetExtension(name).ToLower();
286288

287289
if(cd.Name.StartsWith("generated") && !cd.Name.Equals(dataName)) continue;
288290

289291
if (FileHelper.FileExist(path))
290292
{
291293
if (!archive.Entries.Any(entry => entry.Name.EndsWith(name)))
294+
{
295+
if (cd.Name.Equals("metadata"))
296+
{
297+
name = IOHelper.GetFileName(FileType.Metadata, id, datasetVersionNumber, dataStructureId) + ext;
298+
}
299+
else if (cd.Name.Equals("datastructure"))
300+
{
301+
name = IOHelper.GetFileName(FileType.DataStructure, id, datasetVersionNumber, dataStructureId) + ext;
302+
}
303+
else if (cd.Name.Contains("generated"))
304+
{
305+
name = IOHelper.GetFileName(FileType.PrimaryData, id, datasetVersionNumber, dataStructureId) + ext;
306+
}
307+
else
308+
{
309+
name = IOHelper.GetFileName(FileType.None, id, datasetVersionNumber, dataStructureId,cd.Name) + ext;
310+
}
311+
292312
archive.AddFileToArchive(path, name);
313+
}
293314
}
294315
}
295316

@@ -307,16 +328,17 @@ public ActionResult GenerateZip(long id, long versionid, string format)
307328

308329
if (manifest != null)
309330
{
331+
string manifestFileName = IOHelper.GetFileName(FileType.Manifest, id, datasetVersionNumber, dataStructureId);
310332
string manifestPath = OutputDatasetManager.GetDynamicDatasetStorePath(id,
311-
datasetVersionNumber, "manifest", ".json");
333+
datasetVersionNumber, manifestFileName, ".json");
312334
string fullFilePath = Path.Combine(AppConfiguration.DataPath, manifestPath);
313335
string directory = Path.GetDirectoryName(fullFilePath);
314336
if (!Directory.Exists(directory))
315337
FileHelper.CreateDicrectoriesIfNotExist(directory);
316338

317339
System.IO.File.WriteAllText(fullFilePath, manifest, System.Text.Encoding.UTF8);
318340

319-
archive.AddFileToArchive(fullFilePath, "manifest.json");
341+
archive.AddFileToArchive(fullFilePath, manifestFileName + ".json");
320342
}
321343
#endregion
322344

@@ -329,7 +351,7 @@ public ActionResult GenerateZip(long id, long versionid, string format)
329351

330352
#endregion terms and conditions
331353

332-
string title = datasetVersion.Title;
354+
title = datasetVersion.Title;
333355
title = String.IsNullOrEmpty(title) ? "unknown" : title;
334356

335357
string message = string.Format("dataset {0} version {1} was downloaded as zip - {2}.", id,
@@ -406,6 +428,12 @@ private string storeGeneratedFilePathToContentDiscriptor(long datasetId, Dataset
406428
mimeType = "text/comma-separated-values";
407429
}
408430

431+
if (ext.Contains("json"))
432+
{
433+
name = "datastructure";
434+
mimeType = "application/json";
435+
}
436+
409437
if (ext.Contains("html"))
410438
{
411439
name = title;
@@ -436,6 +464,8 @@ private string storeGeneratedFilePathToContentDiscriptor(long datasetId, Dataset
436464
{
437465
if (cd.Name.Equals(name) && cd.MimeType.Equals(mimeType))
438466
{
467+
cd.Name = name;
468+
cd.MimeType = mimeType;
439469
cd.URI = dynamicPath;
440470
dm.UpdateContentDescriptor(cd);
441471
}
@@ -463,7 +493,7 @@ private void generateMetadataAsHtml(DatasetVersion dsv)
463493

464494
string title = dsv.Title;
465495
Session["ShowDataMetadata"] = dsv.Metadata;
466-
496+
int versionNr = 0;
467497
var view = this.Render("DCM", "Form", "LoadMetadataOfflineVersion", new RouteValueDictionary()
468498
{
469499
{ "entityId", datasetId },
@@ -477,6 +507,7 @@ private void generateMetadataAsHtml(DatasetVersion dsv)
477507

478508
byte[] content = Encoding.ASCII.GetBytes(view.ToString());
479509

510+
480511
string dynamicPathOfMD = "";
481512
dynamicPathOfMD = storeGeneratedFilePathToContentDiscriptor(datasetId, dsv,
482513
"metadata", ".html");
@@ -493,7 +524,7 @@ private void generateDataStructureHtml(DatasetVersion dsv)
493524
});
494525

495526
byte[] content = Encoding.ASCII.GetBytes(view.ToString());
496-
527+
497528
string dynamicPathOfMD = "";
498529
dynamicPathOfMD = storeGeneratedFilePathToContentDiscriptor(dsv.Dataset.Id, dsv,
499530
"datastructure", ".html");

0 commit comments

Comments
 (0)