fileUtil.js
7.7 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import * as fetch from './fetch';
import * as constants from '../redux/constants/Constants';
const FILEMETA_PATH='/file_api/filemeta'; //获取文件
/*
添加文件获取Token
HRO现有业务类型:crm (客户管理)、recruit (招聘管理)、contract(合同管理)、user_image(个人图像)、tenant_logo(企业logo)
object_type string 业务类型 Yes
access_type string 接入类别, 比如sdk\web_upload Yes
*/
const getTokenForFile=(object_type,access_type='web_upload',options={})=>{
const url=FILEMETA_PATH+'/inits';
return fetch.post(url,{
object_type,
access_type
},options);
}
//url split bucket
const splitBucket=(url)=>{
let tempUrl=url,bucket='',filePath='';
if(url&&url.indexOf("/")==0){
tempUrl=url.substring(1);
}
bucket=tempUrl.substring(0,tempUrl.indexOf("/"));
filePath=tempUrl.substring(tempUrl.indexOf("/")+1);
return {
bucket,
path:filePath
}
}
//download File
const downloadFileByUrl=(url)=>{
const fileObject=splitBucket(url);
if(fileObject.bucket==`${window.oss.bucket}`){
window.open('/filemeta?t='+new Date().getTime()+'&bucket='+fileObject.bucket+'&object='+fileObject.path);
}else{
window.open(`http://${window.oss.public_bucket}.${window.oss.end_point}/${fileObject.path}`);
}
}
//下载对象重定向
const getDownloadRedirect=(fileObject)=>{
return '/filemeta?t='+new Date().getTime()+'&bucket='+fileObject.bucket+'&object='+fileObject.path;
}
//下载对象返回JSON
const getDownloadPath=(fileObject)=>{
return FILEMETA_PATH+'/object-redirect'+'?t='+new Date().getTime()+'&bucket='+fileObject.bucket+'&object='+fileObject.path;
}
const deleteFile = (fileId) => {
return fetch.post(
'files/delete',
{'file_id':fileId});
}
// const getToken =(file_id)=>{
// return fetch.post('/getObjectTokenByID',{file_id},);
// }
const getTokenForPdf =(path)=>{
return fetch.post('/api/tokens',{"object_path":path},constants.METADATA);
}
const downloadFile = (bucket, path, name,file_id) =>{
if(!file_id)
return
const w=window.open('about:blank');
getToken(file_id).then(data=>{
var client = new OSS({
endpoint: `http://${window.oss.end_point}`,
accessKeyId: data.access_key_id,//'SX9nVDsfn54Tsl2K',
accessKeySecret: data.access_key_secret,//'VoYVvrMZyWBfVG9sBgA2UCqSczJnoL',
stsToken: data.security_token,
bucket:bucket
});
path = path;
console.log(path);
var url = client.signatureUrl(path, {
expires: 3600,
'content-disposition': 'attachment; filename="' + name + '"'
});
console.log(url);
w.location.href=url;
})
}
const showPdfView = (path) =>{
if(!path)
return
const w=window.open('about:blank');
getTokenForPdf(path).then(data=>{
var client = new OSS({
endpoint: `http://${window.oss.end_point}`,
accessKeyId: data.access_key_id,//'SX9nVDsfn54Tsl2K',
accessKeySecret: data.access_key_secret,//'VoYVvrMZyWBfVG9sBgA2UCqSczJnoL',
stsToken: data.security_token,
// bucket:data.bucket
});
path = path;
console.log(path);
var url = client.signatureUrl(path, {
expires: 3600,
'content-disposition': 'attachment; filename="' + name + '"'
});
console.log(url);
// w.location.href='/hr/viewPdf?pdfPath='+url;
w.location.href=url;
})
}
const showPic = (bucket, path, name,file_id) =>{
console.log(!file_id)
if(file_id){
// const w=window.open('about:blank');
let url;
getToken(file_id).then(data=>{
var client = new OSS({
endpoint: `http://${window.oss.end_point}`,
accessKeyId: data.access_key_id,//'SX9nVDsfn54Tsl2K',
accessKeySecret: data.access_key_secret,//'VoYVvrMZyWBfVG9sBgA2UCqSczJnoL',
stsToken: data.security_token,
bucket:bucket
});
path = path;
console.log(path);
url = client.signatureUrl(path, {
expires: 3600,
'content-disposition': 'attachment; filename="' + name + '"'
});
console.log(url);
// w.location.href=url;
})
return url
}
}
//下载云表格
const cloudDown = (data)=>{//云下载方法
const w=window.open('about:blank');
var client = new OSS({
endpoint:`http://${window.oss.end_point}`,
accessKeyId: data.access_key_id,
accessKeySecret: data.access_key_secret,
stsToken: data.security_token,
bucket:data.bucket
});
let path=data.object_path;
console.log(path);
let name='employeeTableData';
var url = client.signatureUrl(path,{
expires: 3600,
'content-disposition': 'attachment; filename="' + name + '"'
});
console.log(url);
window.location.href=url;
}
const downloadForm=(sn,formName)=>{
const serviceName=getServiceUrl(sn);
const w=window.open('about:blank');
fetch.post('/files/exportFormExcel',{serviceName,formName}).then(data=>{
var client = new OSS({
endpoint: "http://"+data.domain,
accessKeyId: data.access_key_id,//'SX9nVDsfn54Tsl2K',
accessKeySecret: data.access_key_secret,//'VoYVvrMZyWBfVG9sBgA2UCqSczJnoL',
stsToken: data.security_token,
bucket:data.bucket
});
const path = data.object_path;
console.log(path);
var url = client.signatureUrl(path, {
expires: 3600,
'content-disposition': 'attachment; filename="' + formName + '"'
});
console.log(url);
w.location.href=url;
})
}
// const downloadFile = (bucket, path, name) =>{
// co(function* () {
// var token = yield sts.assumeRole(
// '<role-arn>', policy, 15 * 60, '<session-name>');
// var client = new OSS({
// endpoint: "http://oss-cn-beijing.aliyuncs.com",
// accessKeyId: 'STS.JDiY8ogubG1z5BEd4HJ31d4Bh',
// accessKeySecret: "9798iZyPUJTqW5m6xkHYqGyeNX5Ev8CJKsxrXbkGt1uR",
// stsToken: "CAESpgMIARKAAX1kQtRJSYaCH+RwupSBTEpiFoq8+Di6ZKPxUhqQ+pgQttQI/tSYmeopxlhCOhlbOFpEtoGMM4ZeEeHk87ypV405lorN4Cf/rrxY6XJ0MuHM76eL21Ib/icnNA+oPkWfN/O8GoerQJN/w8elmPqKcjS3UM4XRnAmy3OpQOHtMou5Gh1TVFMuSkRpWThvZ3ViRzF6NUJFZDRISjMxZDRCaCISMzkxMDI3MzQzMTUzNzc3MzUzKgVoZWxsbzDNq6iCvio6BlJzYU1ENUKGAQoBMRqAAQoFQWxsb3cSNgoMQWN0aW9uRXF1YWxzEgZBY3Rpb24aHgoNb3NzOkdldE9iamVjdAoNb3NzOlB1dE9iamVjdBI/Cg5SZXNvdXJjZUVxdWFscxIIUmVzb3VyY2UaIwohYWNzOm9zczoqOjE5NzYzMjYzNDEyNDMyNTI6aHJveC8qShAxOTc2MzI2MzQxMjQzMjUyUgUyNjg0MloPQXNzdW1lZFJvbGVVc2VyYABqEjM5MTAyNzM0MzE1Mzc3NzM1M3IEaHJveHj04p6s1a7BAw==",
// bucket:bucket
// });
// path = path;
// console.log(path);
// var url = client.signatureUrl(path, {
// expires: 3600,
// 'content-disposition': 'attachment; filename="' + name + '"'
// });
// console.log(url);
// window.open(url);
// }).catch(function (err) {
// console.log(err);
// });
// }
const importData=(file_path,form_name,sn)=>{
const service_name=getServiceUrl(sn);
return fetch.post('/files/uploadfile',{file_path,form_name,service_name});
}
const importColData=(file_path,form_name,sn,field_name)=>{
const service_name=getServiceUrl(sn);
return fetch.post('/files/uploadfileimport',{file_path,form_name,service_name,field_name});
}
const doMapping=(params)=>{
return fetch.post('/hr/doMapping',params);
}
const doRepeat=(params)=>{
return fetch.post('/hr/handleDuplicate',params);
}
const doConflict=(params)=>{
return fetch.post('/hr/handleConflict',params);
}
const getServiceUrl = (service)=>{
var serviceUrl = '';
switch(service){
case 'hr':
serviceUrl = "hrUrl";
break;
default:
break;
}
return serviceUrl;
}
const urlEncode=(str)=>{
return encodeURI(str);
}
const urlDecode=(str)=>{
return decodeURI(str);
}
const getToken =(file_id)=>{
return fetch.get(`/api/objects/${file_id}`,constants.METADATA);
}
const getPDFJson = (url,options={})=>{
return fetch.get(url, options);
}
export default{
deleteFile,
downloadFile,
downloadForm,
cloudDown,
importData,
importColData,
doMapping,
doRepeat,
doConflict,
getToken,
showPic,
urlEncode,
urlDecode,
showPdfView,
getTokenForFile,
getDownloadPath,
getDownloadRedirect,
downloadFileByUrl,
splitBucket,
getPDFJson
};