Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export type Source = {
method?: string;
};

export type PdfError = Error & {
status?: number;
};

export type TextSelectionChangeEvent = {
nativeEvent:
| {
Expand Down Expand Up @@ -74,7 +78,7 @@ export interface PdfProps {
onLoadProgress?: (percent: number,) => void,
onLoadComplete?: (numberOfPages: number, path: string, size: {height: number, width: number}, tableContents?: TableContent[]) => void,
onPageChanged?: (page: number, numberOfPages: number) => void,
onError?: (error: object) => void,
onError?: (error: PdfError) => void,
onPageSingleTap?: (page: number, x: number, y: number) => void,
onScaleChanged?: (scale: number) => void,
onPressLink?: (url: string) => void,
Expand Down
21 changes: 17 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,14 @@ export default class Pdf extends Component {
.then(async (res) => {

this.lastRNBFTask = null;
const responseInfo = res ? res.respInfo : undefined;

if (res && res.respInfo && res.respInfo.headers && !res.respInfo.headers["Content-Encoding"] && !res.respInfo.headers["Transfer-Encoding"] && res.respInfo.headers["Content-Length"]) {
const expectedContentLength = res.respInfo.headers["Content-Length"];
if (responseInfo && typeof responseInfo.status === "number" && (responseInfo.status < 200 || responseInfo.status >= 300)) {
throw this._createDownloadError(source.uri, responseInfo);
}

if (responseInfo && responseInfo.headers && !responseInfo.headers["Content-Encoding"] && !responseInfo.headers["Transfer-Encoding"] && responseInfo.headers["Content-Length"]) {
const expectedContentLength = responseInfo.headers["Content-Length"];
let actualContentLength;

try {
Expand All @@ -306,11 +311,11 @@ export default class Pdf extends Component {

actualContentLength = fileStats.size;
} catch (error) {
throw new Error("DownloadFailed:" + source.uri);
throw this._createDownloadError(source.uri, responseInfo);
}

if (expectedContentLength != actualContentLength) {
throw new Error("DownloadFailed:" + source.uri);
throw this._createDownloadError(source.uri, responseInfo);
}
}

Expand All @@ -335,6 +340,14 @@ export default class Pdf extends Component {

};

_createDownloadError = (uri, responseInfo) => {
const error = new Error("DownloadFailed:" + uri);
if (responseInfo) {
error.status = responseInfo.status;
}
return error;
};

_unlinkFile = async (file) => {
try {
await ReactNativeBlobUtil.fs.unlink(file);
Expand Down