-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportFileCommandHandler.cs
More file actions
98 lines (81 loc) · 3.77 KB
/
Copy pathExportFileCommandHandler.cs
File metadata and controls
98 lines (81 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using ShapeDiver.SDK.Stargate;
using ShapeDiver.SDK.Stargate.Commands.Interfaces;
using ShapeDiver.SDK.Stargate.Dto.Commands;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace StargateDotNetClientExample
{
/// <summary>
/// This handler is called by the ShapeDiver Platform when the user wants to export
/// a file from the ShapeDiver model.
/// </summary>
internal class ExportFileCommandHandler : BaseCommandHandler, IExportFileCommandHandler
{
public ExportFileCommandHandler(IServices services) : base(services)
{
}
/// <summary>
/// Supported content types for file exports.
/// TODO extend this list as required.
/// </summary>
List<string> supportedContentTypes = new List<string>()
{
"application/dwg",
"application/json"
};
public async Task<ExportFileReplyDto> Handle(ExportFileCommandDto data)
{
Services.LogMessage("Handling ExportFileCommand");
// get session context for model
var sessionContext = await this.Services.GeometryBackendContextCache.GetContextAsync(data.Model.Id, false, ModelTokenScopes);
// verify that requested export id exists for the model
if (!sessionContext.ModelData.Exports.ContainsKey(data.Export.Id))
{
throw new StargateException($"An export with id {data.Export.Id} does not exist for model {data.Model.Id}.");
}
// get export results
var computationResult = await this.Services.Sdk.GeometryBackendClient.ComputeExport(sessionContext, data.Export.Id, data.Parameters);
// find exported file
var allExportedAssets = this.Services.Sdk.GeometryBackendClient.GetAllExportAssets(sessionContext, computationResult);
var exportedAssets = allExportedAssets.Where(asset => asset.ExportId == data.Export.Id).ToList();
if (exportedAssets.Count == 0)
{
throw new StargateException($"Export result is empty.");
}
var exportedAsset = exportedAssets.First();
// verify content type of file is supported
if (!supportedContentTypes.Contains(exportedAsset.ContentType))
{
throw new StargateException($"Exporting files with content type '{exportedAsset.ContentType}' is not supported.");
}
// download the file
var stream = await exportedAsset.GetStream();
if (exportedAsset.ContentType == "application/json")
{
/// TODO: Use exported JSON data available in memory stream.
return Reply(ExportFileResultEnum.Success, $"Handled export of JSON file ({stream.Length} bytes)");
}
// DWG
else if (exportedAsset.ContentType == "application/dwg")
{
/// TODO: Use exported CAD file (available in memory stream, DWG file format).
return Reply(ExportFileResultEnum.Success, $"Handled export of DWG file ({stream.Length} bytes)");
}
// TODO: In case the user can cancel the export operation, or errors might happen during the export,
// consider replying with other status codes (see ExportFileResultEnum)
return Reply(ExportFileResultEnum.Failure, $"This should not happen");
}
private ExportFileReplyDto Reply(ExportFileResultEnum result, string message = null)
{
return new ExportFileReplyDto()
{
Info = new ExportFileInfoReplyDto()
{
Result = result,
Message = message
}
};
}
}
}