-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
161 lines (124 loc) · 6.02 KB
/
Copy pathProgram.cs
File metadata and controls
161 lines (124 loc) · 6.02 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using VVRestApi.Common;
using VVRestApi.Vault;
using System.Configuration;
using VVRestApi.Vault.Library;
using System.IO;
using System.Text.RegularExpressions;
namespace DocFileExamples
{
internal class Program
{
static void Main(string[] args)
{
//vaultClient will be null if authentication fails
var vaultClient = VaultApiLogin();
const string providerId = "PROV-001670";
var documentList = GetProviderDocuments(vaultClient, providerId);
foreach (var doc in documentList)
{
GetDocumentMetaData(doc);
GetDocumentRevisionFile(vaultClient, doc);
}
}
private static VaultApi VaultApiLogin()
{
var clientSecrets = new ClientSecrets
{
BaseUrl = ConfigurationManager.AppSettings["BaseUrl"],
CustomerAlias = ConfigurationManager.AppSettings["CustomerAlias"],
DatabaseAlias = ConfigurationManager.AppSettings["DatabaseAlias"],
ApiKey = ConfigurationManager.AppSettings["ApiKey"],
ApiSecret = ConfigurationManager.AppSettings["ApiSecret"],
ApiVersion = "1",
Scope = "vault",
OAuthTokenEndPoint = ConfigurationManager.AppSettings["OAuthTokenEndPoint"]
};
var vaultClient = new VaultApi(clientSecrets);
return vaultClient;
}
private static List<Document> GetProviderDocuments(VaultApi vaultApi, string providerId)
{
var folderPath = $"/Provider Licensing/Providers/{providerId}";
var docList = vaultApi.Documents.GetDocumentsBySearch(new RequestOptions { Query = $"[folderPath] eq '{folderPath}'", Expand = true });
//note: you can execute this http get in the browser using this URL
//https://dcfplads.visualvault.com/api/v1/FLDCF/SAMH/documents?q = folderPath eq '/Provider Licensing/Providers/PROV-001670'
//search query syntax (OData) can be found at
//https://docs.visualvault.com/docs/query-syntax
return docList;
}
private static void GetDocumentMetaData(Document doc)
{
if (doc != null)
{
//revision neutral Id, always returns latest revision of the document
Guid documentId = doc.DocumentId;
//revision specific Id, can be used to get a specific document revision that may not be latest
Guid revisionId = doc.Id;
Guid folderId = doc.FolderId;
string folderPath = doc.FolderPath;
//id of the file attached to document (each doc revision can have 1 file attached)
Guid fileId = doc.FileId;
//file name includes the file extension
string fileName = doc.Filename;
//file extension does not begin with "."
string fileExtension = doc.Extension;
//MIME Type such as application/pdf
string contentType = doc.ContentType;
//always the create date of revision 1
DateTime createDate = doc.CreateDate;
//create date of revision n, for rev 1 will match createDate
DateTime modifyDate = doc.ModifyDate;
//revison defaults to integer but can be any unqiue user defined value
string revision = doc.Revision;
}
}
private static void GetDocumentRevisionFile(VaultApi vaultApi, Document doc)
{
//simple example to write file to local disk
//and skip if file exists and is the same size
var stream = vaultApi.Files.GetStream(doc.Id);
var localFilePath = System.IO.Path.GetTempPath();
if (stream != null)
{
//Store the document in the same path as it exists in VisualVault, within the target folder specified in the download configuration settings
var targetFolderPath = localFilePath + doc.FolderPath;
//remove any characters not valid for local file system
var vvFolderPathFolderNamesPattern = @"\/([^\/]+)";
var folderPathFolderNames = Regex.Matches(targetFolderPath, vvFolderPathFolderNamesPattern);
if (targetFolderPath.Length + doc.Filename.Length >= 200)
{
//attempt to shorten path if too long for local file system
var winFileSystemFolderPathPattern = @".*\\([^\/]+)";
var winFolderPath = Regex.Matches(targetFolderPath, winFileSystemFolderPathPattern);
//shorten path to avoid exception. NT file system can only handle 260 char path.
targetFolderPath = winFolderPath[0] + folderPathFolderNames[0].ToString().Replace(@"/", @"\") + @"\";
}
if (!Directory.Exists(targetFolderPath))
{
Directory.CreateDirectory(targetFolderPath);
}
string docPath = targetFolderPath + "\\" + doc.Filename;
if (File.Exists(docPath))
{
//check file size and if identical then skip
//allows restart of downloads
var fileInfo = new FileInfo(docPath);
if (fileInfo.Length != doc.FileSize)
{
File.Delete(docPath);
}
if (!File.Exists(docPath))
{
using (var fileStream = File.Create(docPath))
{
//can also read file stream from VV in chunks for large files
stream.CopyTo(fileStream);
}
}
}
}
}
}
}