Fix opening video when file has localPath set (#1817)

This commit is contained in:
Elias Nahum 2018-06-25 12:30:03 -04:00 committed by GitHub
parent 763016cf1a
commit 6446b370d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 49 deletions

View file

@ -275,65 +275,51 @@ export default class Downloader extends PureComponent {
startDownload = async () => {
const {file, downloadPath, prompt, saveToCameraRoll} = this.props;
const {data} = file;
let downloadFile = true;
try {
if (this.state.didCancel) {
this.setState({didCancel: false});
}
let path;
let res;
if (data && data.localPath) {
path = data.localPath;
downloadFile = false;
this.setState({
progress: 100,
started: true,
});
}
const imageUrl = Client4.getFileUrl(data.id);
const options = {
session: data.id,
timeout: 10000,
indicator: true,
overwrite: true,
};
if (downloadFile) {
const imageUrl = Client4.getFileUrl(data.id);
const options = {
session: data.id,
timeout: 10000,
indicator: true,
overwrite: true,
};
if (downloadPath && prompt) {
const isDir = await RNFetchBlob.fs.isDir(downloadPath);
if (!isDir) {
try {
await RNFetchBlob.fs.mkdir(downloadPath);
} catch (error) {
this.showDownloadFailedAlert();
return;
}
if (downloadPath && prompt) {
const isDir = await RNFetchBlob.fs.isDir(downloadPath);
if (!isDir) {
try {
await RNFetchBlob.fs.mkdir(downloadPath);
} catch (error) {
this.showDownloadFailedAlert();
return;
}
options.path = `${downloadPath}/${data.id}-${file.caption}`;
} else {
options.fileCache = true;
options.appendExt = data.extension;
}
this.downloadTask = RNFetchBlob.config(options).fetch('GET', imageUrl);
this.downloadTask.progress((received, total) => {
const progress = (received / total) * 100;
if (this.mounted) {
this.setState({
progress,
started: true,
});
}
});
res = await this.downloadTask;
path = res.path();
options.path = `${downloadPath}/${data.id}-${file.caption}`;
} else {
options.fileCache = true;
options.appendExt = data.extension;
}
this.downloadTask = RNFetchBlob.config(options).fetch('GET', imageUrl);
this.downloadTask.progress((received, total) => {
const progress = (received / total) * 100;
if (this.mounted) {
this.setState({
progress,
started: true,
});
}
});
const res = await this.downloadTask;
let path = res.path();
if (saveToCameraRoll) {
path = await CameraRoll.saveToCameraRoll(path, 'photo');
}

View file

@ -40,6 +40,12 @@ export default class VideoPreview extends PureComponent {
constructor(props) {
super(props);
let path = null;
const {file} = props;
if (file && file.data && file.data.localPath) {
path = file.data.localPath;
}
this.state = {
isLoading: true,
isFullScreen: false,
@ -47,7 +53,7 @@ export default class VideoPreview extends PureComponent {
paused: true,
currentTime: 0,
duration: 0,
path: null,
path,
showDownloader: true,
};
}
@ -84,7 +90,7 @@ export default class VideoPreview extends PureComponent {
onDownloadSuccess = () => {
const {file} = this.props;
const path = `${VIDEOS_PATH}/${file.data.id}-${file.caption}`;
const path = file.data.localPath || `${VIDEOS_PATH}/${file.data.id}-${file.caption}`;
this.setState({showDownloader: false, path});
};