@boristype/ws-client 0.1.0-alpha.0 → 0.1.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@boristype/ws-client",
3
- "version": "0.1.0-alpha.0",
3
+ "version": "0.1.0-alpha.1",
4
4
  "description": "WSHCM SOAP client — клиент для работы с WebSoft HCM через SP-XML API",
5
5
  "type": "module",
6
6
  "main": "build/index.js",
7
7
  "types": "build/index.d.ts",
8
8
  "files": [
9
9
  "build",
10
+ "resources",
10
11
  "README.md"
11
12
  ],
12
13
  "dependencies": {
@@ -0,0 +1,267 @@
1
+ // имя целевого файла для загрузки (обычный файл или директория)
2
+ var sFileName = '{{file_name}}' // string
3
+ // путь назначения для загрузки
4
+ var sDestPath = '{{dest_path}}' // string
5
+ // временная директория на всю сессию процесса загрузки файлов
6
+ var sTempPath = '{{temp_path}}' // string
7
+ // перечень относительных путей всех загружаемых файлов
8
+ var sarrFileList = ParseJson('{{file_paths}}'); // string[]
9
+
10
+ function main() {
11
+ joinAllFiles();
12
+
13
+ if (!IsEmptyValue(sDestPath)) {
14
+ // Путь назначения указан, переместить файлы по этому пути и вернуть url'ы на них
15
+ return copyContentToDestination();
16
+ } else {
17
+ // Путь назначения НЕ указан, вернуть url'ы на файлы во временной директории
18
+ return getUploadedFileUrls();
19
+ }
20
+ }
21
+
22
+ function joinAllFiles() {
23
+ var sFilePath;
24
+ for (sFilePath in sarrFileList) {
25
+ joinChunks(sFilePath);
26
+ }
27
+ }
28
+
29
+ function joinChunks(sFilePath) {
30
+ var sFileHash = StrUpperCase(Md5Hex(sFilePath));
31
+ // path: <...tmp>/chunks/<file_hash = md5(file_path...)/chunk_>
32
+ var sChunkPath = FilePath(FilePath(FilePath(sTempPath, "chunks"), sFileHash), "chunk_");
33
+ // path: <...tmp>/files/<file_path...>
34
+ var sMirrorFilePath = UrlToFilePath(UrlAppendPath(FilePathToUrl(FilePath(sTempPath, "files")), sFilePath));
35
+
36
+ ObtainDirectory(ParentDirectory(sMirrorFilePath), true)
37
+ PutFileData(sMirrorFilePath, '');
38
+
39
+ var iChunkNumber = 0;
40
+ var iChunkSize = 0;
41
+ var iStartIndex = 0;
42
+ var iFinishIndex = 0;
43
+ var sbChunkData;
44
+ var sPresentChunkPath;
45
+ while (true) {
46
+ sPresentChunkPath = sChunkPath + iChunkNumber;
47
+ // alert(sPresentChunkPath)
48
+
49
+ try {
50
+ sbChunkData = LoadFileData(sPresentChunkPath);
51
+ } catch (err) {
52
+ break;
53
+ }
54
+
55
+ iChunkSize = FileSize(sPresentChunkPath);
56
+ iFinishIndex = iStartIndex + iChunkSize;
57
+ // alert('chunk write [' + chunkNumber + ']: ' + startIndex + '-' + finishIndex)
58
+ PutFileDataRange(sMirrorFilePath, iStartIndex, iFinishIndex, sbChunkData);
59
+ iStartIndex = iFinishIndex;
60
+
61
+ iChunkNumber += 1;
62
+ }
63
+
64
+ DeleteDirectory(ParentDirectory(sChunkPath))
65
+ }
66
+
67
+ function copyContentToDestination() {
68
+ if (!IsEmptyValue(sFileName)) {
69
+ var sContentPath = UrlToFilePath(UrlAppendPath(FilePathToUrl(FilePath(sTempPath, "files")), sFileName));
70
+ var sFinalPath = getFinalDestinationFilePath(sContentPath);
71
+
72
+ if (sFinalPath.error) {
73
+ deleteAnyFile(sContentPath);
74
+ throw sFinalPath.message;
75
+ } else {
76
+ copyAnyFile(sContentPath, sFinalPath.value);
77
+ deleteAnyFile(sContentPath);
78
+ return ok([FilePathToUrl(sFinalPath.value)]);
79
+ }
80
+ } else {
81
+ // glob режим (dir/*): копируем содержимое напрямую в destination
82
+ var sResolvedDestPath = resolveDestPath();
83
+ var sFilesPath = FilePath(sTempPath, "files");
84
+ var sarrUrls = [];
85
+
86
+ var sFilePath;
87
+ var sSrcPath;
88
+ var sDstPath;
89
+ for (sFilePath in sarrFileList) {
90
+ sSrcPath = UrlToFilePath(UrlAppendPath(FilePathToUrl(sFilesPath), sFilePath));
91
+ sDstPath = UrlToFilePath(UrlAppendPath(FilePathToUrl(sResolvedDestPath), sFilePath));
92
+ copyAnyFile(sSrcPath, sDstPath);
93
+ deleteAnyFile(sSrcPath);
94
+ sarrUrls.push(FilePathToUrl(sDstPath));
95
+ }
96
+
97
+ return ok(sarrUrls);
98
+ }
99
+ }
100
+
101
+ function getUploadedFileUrls() {
102
+ if (!IsEmptyValue(sFileName)) {
103
+ var sContentPath = UrlToFilePath(UrlAppendPath(FilePathToUrl(FilePath(sTempPath, "files")), sFileName));
104
+ return ok([FilePathToUrl(sContentPath)]);
105
+ } else {
106
+ // glob режим (dir/*): возвращаем URL'ы всех файлов
107
+ var sFilesPath = FilePath(sTempPath, "files");
108
+ var sarrUrls = [];
109
+
110
+ var sFilePath;
111
+ for (sFilePath in sarrFileList) {
112
+ var sContentPath = UrlToFilePath(UrlAppendPath(FilePathToUrl(sFilesPath), sFilePath));
113
+ sarrUrls.push(FilePathToUrl(sContentPath));
114
+ }
115
+
116
+ return ok(sarrUrls);
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Разрешает путь назначения (x-local:// -> AppDirectoryPath)
122
+ */
123
+ function resolveDestPath() {
124
+ if (sDestPath === "x-local://") {
125
+ return AppDirectoryPath();
126
+ }
127
+ try {
128
+ return UrlToFilePath(sDestPath);
129
+ } catch (err) {
130
+ return sDestPath;
131
+ }
132
+ }
133
+
134
+ function getFinalDestinationFilePath(sContentPath) {
135
+ if (sDestPath === "x-local://") {
136
+ sDestPath = AppDirectoryPath();
137
+ } else {
138
+ try {
139
+ sDestPath = UrlToFilePath(sDestPath);
140
+ } catch (err) { }
141
+ }
142
+
143
+ if (FilePathExists(sDestPath)) {
144
+ // sDestPath существует
145
+
146
+ if (IsDirectory(sDestPath)) {
147
+ // sDestPath - директория (существует)
148
+
149
+ if (IsDirectory(sContentPath)) {
150
+ // sContentPath - директория
151
+
152
+ // 1. директория -> директория (существует)
153
+ // директория sContentPath будет записана внутри директории sDestPath
154
+ return ok(FilePath(sDestPath, sFileName));
155
+ } else {
156
+ // sContentPath - файл
157
+
158
+ // 2. файл -> директория (существует)
159
+ // файл sContentPath будет записан внутри директории sDestPath
160
+ return ok(FilePath(sDestPath, sFileName));
161
+ }
162
+ } else {
163
+ // sDestPath - файл (существует)
164
+
165
+ if (IsDirectory(sContentPath)) {
166
+ // sContentPath - директория
167
+
168
+ // 3. директория -> файл (существует)
169
+ // ERR: нельзя перезаписать директорию в существующий файл
170
+ return fail("cannot overwrite non-directory '" + sDestPath + "' with directory 'sFileName'");
171
+ } else {
172
+ // sContentPath - файл
173
+
174
+ // 4. файл -> файл (существует)
175
+ // файл sContentPath перезапишет существующий файл sDestPath
176
+ return ok(sDestPath);
177
+ }
178
+ }
179
+
180
+ } else {
181
+ var sParentDir = ParentDirectory(sDestPath);
182
+ if (!FilePathExists(sParentDir)) {
183
+ return fail("cannot create directory '" + sDestPath + "': No such file or directory");
184
+ } else if (!IsDirectory(sParentDir)) {
185
+ return fail("cannot create directory '" + sDestPath + "': Not a directory");
186
+ }
187
+
188
+ var sDestUrl = FilePathToUrl(sDestPath)
189
+ if (StrEnds(sDestUrl, "/")) {
190
+ // sDestPath - претендует быть директорией
191
+
192
+ if (IsDirectory(sContentPath)) {
193
+ // sContentPath - директория
194
+
195
+ // 5. директория -> директория (не существует)
196
+ // директория sContentPath будет записана по пути sDestPath
197
+ return ok(sDestPath);
198
+ } else {
199
+ // sContentPath - файл
200
+
201
+ // 6. файл -> директория (не существует)
202
+ // ERR: попытка записать файл внутрь несуществующей директории
203
+ return fail("cannot create regular file '" + sDestPath + "': Not a directory")
204
+ }
205
+ } else {
206
+ // sDestPath - файл или директория
207
+
208
+ if (IsDirectory(sContentPath)) {
209
+ // sContentPath - директория
210
+
211
+ // 7. директория -> файл или директория (не существует)
212
+ // директория sContentPath будет записана по пути sDestPath
213
+ return ok(sDestPath);
214
+ } else {
215
+ // sContentPath - файл
216
+
217
+ // 8. файл -> файл или директория (не существует)
218
+ // файл sContentPath будет записан по пути sDestPath
219
+ return ok(sDestPath);
220
+ }
221
+ }
222
+ }
223
+ }
224
+
225
+ function copyAnyFile(sSrcPath, sDstPath) {
226
+ if (IsDirectory(sSrcPath)) {
227
+ ObtainDirectory(sDstPath);
228
+
229
+ var sarrFiles = ReadDirectoryByPath(sSrcPath);
230
+
231
+ var sFileDest = undefined;
232
+ var sFile;
233
+ for (sFile in sarrFiles) {
234
+ sFileDest = FilePath(sDstPath, FileName(sFile));
235
+ copyAnyFile(sFile, sFileDest);
236
+ }
237
+ } else if (FilePathExists(sSrcPath)) {
238
+ ObtainDirectory(ParentDirectory(sDstPath));
239
+ CopyFile(sSrcPath, sDstPath);
240
+ } else {
241
+ throw "No such file.";
242
+ }
243
+ }
244
+
245
+ function deleteAnyFile(sFilePathToDelete) {
246
+ if (IsDirectory(sFilePathToDelete)) {
247
+ DeleteDirectory(sFilePathToDelete)
248
+ } else if (FilePathExists(sFilePathToDelete)) {
249
+ DeleteFile(sFilePathToDelete);
250
+ } else {
251
+ throw "No such file.";
252
+ }
253
+ }
254
+
255
+ function ok(value) {
256
+ return { "error": false, "value": value };
257
+ }
258
+
259
+ function fail(msg) {
260
+ return { "error": true, "message": msg };
261
+ }
262
+
263
+ try {
264
+ return main();
265
+ } catch (err) {
266
+ return fail(err + '');
267
+ }
@@ -0,0 +1,12 @@
1
+ var dest_path = '{{destination}}';
2
+ try {
3
+ dest_path = UrlToFilePath(dest_path);
4
+ } catch (err) {}
5
+ var sTempPath = ObtainTempDirectoryPath();
6
+ var chunk_path = FilePath(FilePath(FilePath(sTempPath, 'chunks'), 'RELATIVE_PATH_HASH'), 'chunk');
7
+
8
+ return {
9
+ 'dest_path': dest_path,
10
+ 'temp_path': sTempPath,
11
+ 'chunk_path': chunk_path
12
+ };