Skip to content

Commit 478d67b

Browse files
ZhijieZhangjustinjung04
authored andcommitted
Meaningful error message when trying to open a file type supported with WebViewer server enabled (#272)
* Meaningful error message when trying to open a file type supported with WebViewer server enabled * Only warn about file extension can't be determined if there's a docName
1 parent 13b7c2f commit 478d67b

File tree

2 files changed

+81
-43
lines changed

2 files changed

+81
-43
lines changed

src/constants/supportedFiles.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
1-
export const supportedPDFExtensions = ['pdf', 'jpg', 'jpeg', 'png'];
2-
export const supportedOfficeExtensions = ['docx', 'xlsx', 'pptx', 'md'];
3-
export const supportedBlackboxExtensions = ['docx', 'xlsx', 'pptx', 'doc', 'xls', 'csv', 'ppt', 'htm', 'html', 'tif', 'tiff', 'jp2', 'md', 'txt', 'pdf', 'jpg', 'jpeg', 'png', 'rtf', 'odf', 'odt', 'odg', 'odp', 'ods', 'dwg', 'dgn', 'dxf'];
4-
export const supportedExtensions = [...supportedPDFExtensions, ...supportedOfficeExtensions, ...supportedBlackboxExtensions, 'xod'].filter((extension, index, self) => self.indexOf(extension) === index);
5-
export const supportedClientOnlyExtensions = [...supportedPDFExtensions, ...supportedOfficeExtensions];
1+
export const supportedPDFExtensions = [
2+
'pdf',
3+
'jpg',
4+
'jpeg',
5+
'png'
6+
];
7+
export const supportedOfficeExtensions = [
8+
'docx',
9+
'xlsx',
10+
'pptx',
11+
'md'
12+
];
13+
export const supportedClientOnlyExtensions = [
14+
'xod',
15+
...supportedPDFExtensions,
16+
...supportedOfficeExtensions
17+
];
18+
export const supportedBlackboxExtensions = [
19+
...supportedClientOnlyExtensions,
20+
'doc',
21+
'xls',
22+
'csv',
23+
'ppt',
24+
'htm',
25+
'html',
26+
'tif',
27+
'tiff',
28+
'jp2',
29+
'txt',
30+
'rtf',
31+
'odf',
32+
'odt',
33+
'odg',
34+
'odp',
35+
'ods',
36+
'dwg',
37+
'dgn',
38+
'dxf'
39+
];
40+
export const supportedExtensions = [
41+
...supportedClientOnlyExtensions,
42+
...supportedBlackboxExtensions
43+
];

src/helpers/loadDocument.js

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -208,42 +208,54 @@ const getDocOptions = (state, dispatch, streaming) => {
208208
});
209209
};
210210

211+
let engineType;
211212
const getEngineType = state => {
212-
const { engineType, pdftronServer } = state.advanced;
213+
if (engineType) {
214+
return engineType;
215+
}
213216

214217
const docName = getDocName(state);
215-
const fileExtension = getDocumentExtension(docName, engineType);
218+
const fileExtension = getDocumentExtension(docName);
219+
const { pdftronServer } = state.advanced;
220+
221+
engineType = state.advanced.engineType;
222+
if (engineType === engineTypes.AUTO) {
223+
if (fileExtension === 'xod') {
224+
engineType = engineTypes.UNIVERSAL;
225+
} else if (pdftronServer) {
226+
engineType = engineTypes.PDFTRON_SERVER;
227+
} else {
228+
if (docName && !fileExtension) {
229+
console.warn(`File extension cannot be determined from ${docName}. Falling back to pdf`);
230+
}
231+
engineType = engineTypes.PDFNETJS;
232+
}
233+
}
216234

217-
if (engineType) {
218-
return engineType;
219-
} else if (fileExtension === 'xod') {
220-
return engineTypes.UNIVERSAL;
221-
} else if (pdftronServer) {
222-
return engineTypes.PDFTRON_SERVER;
223-
} else if (isPDFNetJSExtension(fileExtension)) {
224-
return engineTypes.PDFNETJS;
225-
} else {
226-
return engineTypes.PDFNETJS;
235+
if (fileExtension) {
236+
if (!supportedExtensions.includes(fileExtension)) {
237+
console.error(`File extension ${fileExtension} from ${docName} is not supported. Please see https://www.pdftron.com/documentation/web/guides/file-format-support for a full list of file formats supported by WebViewer`);
238+
} else if (
239+
engineType === engineTypes.PDFNETJS &&
240+
!supportedClientOnlyExtensions.includes(fileExtension) &&
241+
supportedBlackboxExtensions.includes(fileExtension)
242+
) {
243+
console.error(`File extension ${fileExtension} from ${docName} is only supported by using WebViewer with WebViewer Server. See https://www.pdftron.com/documentation/web/guides/file-format-support for a full list of file formats supported by WebViewer. Visit https://www.pdftron.com/documentation/web/guides/wv-server-deployment for more information about WebViewer Server`);
244+
}
227245
}
246+
247+
return engineType;
228248
};
229249

230-
export const getDocumentExtension = (doc, engineType) => {
231-
let extension;
250+
export const getDocumentExtension = docName => {
251+
let extension = '';
232252

233-
if (doc) {
234-
const result = /\.([a-zA-Z]+)(&|$|\?|#)/.exec(doc);
253+
if (docName) {
254+
const result = /\.([a-zA-Z]+)(&|$|\?|#)/.exec(docName);
235255
extension = result && result[1].toLowerCase();
236256
}
237257

238-
if (extension) {
239-
if (!supportedExtensions.includes(extension)) {
240-
console.error(`File extension ${extension} from ${doc} is not supported.\nWebViewer client only mode supports ${supportedClientOnlyExtensions.join(', ')}.\nWebViewer server supports ${supportedBlackboxExtensions.join(', ')}`);
241-
}
242-
} else if (doc && engineType === engineTypes.AUTO) {
243-
console.warn(`File extension cannot be determined from ${doc}. Falling back to pdf`);
244-
}
245-
246-
return extension ? extension : '';
258+
return extension;
247259
};
248260

249261
export const getDocName = state => {
@@ -252,20 +264,8 @@ export const getDocName = state => {
252264
return filename || path || initialDoc;
253265
};
254266

255-
const isPDFNetJSExtension = extension => {
256-
return isOfficeExtension(extension) || isPDFExtension(extension);
257-
};
258-
259-
export const isOfficeExtension = extension => {
260-
return supportedOfficeExtensions.indexOf(extension) !== -1;
261-
};
262-
263-
export const isPDFExtension = extension => {
264-
return supportedPDFExtensions.indexOf(extension) !== -1;
265-
};
266-
267267
const getDocTypeData = ({ docName, pdfBackendType, officeBackendType, engineType, workerHandlers, pdfWorkerTransportPromise, officeWorkerTransportPromise }) => {
268-
const originalExtension = getDocumentExtension(docName, engineType);
268+
const originalExtension = getDocumentExtension(docName);
269269

270270
let type;
271271
let extension = originalExtension;

0 commit comments

Comments
 (0)