lbry-desktop/ui/redux/actions/file.js

222 lines
6.1 KiB
JavaScript
Raw Normal View History

2021-10-05 22:57:47 +02:00
// @flow
import * as ACTIONS from 'constants/action_types';
import * as ABANDON_STATES from 'constants/abandon_states';
2019-03-05 05:46:57 +01:00
// @if TARGET='app'
import { shell } from 'electron';
2019-03-05 05:46:57 +01:00
// @endif
import Lbry from 'lbry';
import { selectClaimForUri } from 'redux/selectors/claims';
import { doAbandonClaim } from 'redux/actions/claims';
import { batchActions } from 'util/batch-actions';
import { doHideModal } from 'redux/actions/app';
import { goBack } from 'connected-react-router';
2019-08-14 05:56:11 +02:00
import { doSetPlayingUri } from 'redux/actions/content';
import { selectPlayingUri } from 'redux/selectors/content';
2021-10-05 22:57:47 +02:00
import { doToast } from 'redux/actions/notifications';
import { selectBalance } from 'redux/selectors/wallet';
import {
makeSelectFileInfoForUri,
selectDownloadingByOutpoint,
makeSelectStreamingUrlForUri,
} from 'redux/selectors/file_info';
2021-10-05 22:57:47 +02:00
type Dispatch = (action: any) => any;
type GetState = () => { file: FileState };
export function doOpenFileInFolder(path: string) {
return () => {
shell.showItemInFolder(path);
};
}
2021-10-05 22:57:47 +02:00
export function doOpenFileInShell(path: string) {
return (dispatch: Dispatch) => {
2020-10-16 01:06:05 +02:00
const success = shell.openPath(path);
if (!success) {
dispatch(doOpenFileInFolder(path));
}
2017-06-06 23:19:12 +02:00
};
}
2021-10-05 22:57:47 +02:00
export function doDeleteFile(outpoint: string, deleteFromComputer?: boolean, abandonClaim?: boolean, cb: any) {
return (dispatch: Dispatch) => {
if (abandonClaim) {
const [txid, nout] = outpoint.split(':');
dispatch(doAbandonClaim(txid, Number(nout), cb));
}
2020-03-27 17:49:41 +01:00
// @if TARGET='app'
2020-04-28 00:22:09 +02:00
Lbry.file_delete({
outpoint,
delete_from_download_dir: deleteFromComputer,
});
dispatch({
type: ACTIONS.FILE_DELETE,
data: {
2017-06-06 23:19:12 +02:00
outpoint,
},
});
2020-03-27 17:49:41 +01:00
// @endif
};
}
2021-10-05 22:57:47 +02:00
export function doDeleteFileAndMaybeGoBack(
uri: string,
deleteFromComputer?: boolean,
abandonClaim?: boolean,
doGoBack: (any) => void
) {
return (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const playingUri = selectPlayingUri(state);
const { outpoint } = makeSelectFileInfoForUri(uri)(state) || '';
const { nout, txid } = selectClaimForUri(state, uri);
const claimOutpoint = `${txid}:${nout}`;
const actions = [];
if (!abandonClaim) {
actions.push(doHideModal());
}
actions.push(
doDeleteFile(outpoint || claimOutpoint, deleteFromComputer, abandonClaim, (abandonState) => {
if (abandonState === ABANDON_STATES.DONE) {
if (abandonClaim) {
if (doGoBack) {
dispatch(goBack());
}
dispatch(doHideModal());
}
}
})
);
if (playingUri && playingUri.uri === uri) {
2020-12-21 16:31:34 +01:00
actions.push(doSetPlayingUri({ uri: null }));
}
2019-08-30 16:01:39 +02:00
// it would be nice to stay on the claim if you just want to delete it
// we need to alter autoplay to not start downloading again after you delete it
dispatch(batchActions(...actions));
2017-06-06 23:19:12 +02:00
};
}
2021-10-05 22:57:47 +02:00
export function doFileGet(uri: string, saveFile: boolean = true, onSuccess?: (GetResponse) => any) {
return (dispatch: Dispatch, getState: () => any) => {
const state = getState();
const { nout, txid } = selectClaimForUri(state, uri);
2021-10-05 22:57:47 +02:00
const outpoint = `${txid}:${nout}`;
dispatch({
type: ACTIONS.FETCH_FILE_INFO_STARTED,
data: {
outpoint,
},
});
// set save_file argument to True to save the file (old behaviour)
Lbry.get({ uri, save_file: saveFile })
.then((streamInfo: GetResponse) => {
const timeout = streamInfo === null || typeof streamInfo !== 'object' || streamInfo.error === 'Timeout';
if (timeout) {
dispatch({
type: ACTIONS.FETCH_FILE_INFO_FAILED,
data: { outpoint },
});
dispatch(doToast({ message: `File timeout for uri ${uri}`, isError: true }));
} else {
if (streamInfo.purchase_receipt || streamInfo.content_fee) {
dispatch({
type: ACTIONS.PURCHASE_URI_COMPLETED,
data: { uri, purchaseReceipt: streamInfo.purchase_receipt || streamInfo.content_fee },
});
}
dispatch({
type: ACTIONS.FETCH_FILE_INFO_COMPLETED,
data: {
fileInfo: streamInfo,
outpoint: outpoint,
},
});
if (onSuccess) {
onSuccess(streamInfo);
}
}
})
.catch((error) => {
dispatch({
type: ACTIONS.PURCHASE_URI_FAILED,
data: { uri, error },
});
dispatch({
type: ACTIONS.FETCH_FILE_INFO_FAILED,
data: { outpoint },
});
dispatch(
doToast({
message: `Failed to view ${uri}, please try again. If this problem persists, visit https://lbry.com/faq/support for support.`,
isError: true,
})
);
});
};
}
export function doPurchaseUri(
uri: string,
costInfo: { cost: number },
saveFile: boolean = true,
onSuccess?: (GetResponse) => any
) {
return (dispatch: Dispatch, getState: GetState) => {
dispatch({
type: ACTIONS.PURCHASE_URI_STARTED,
data: { uri },
});
const state = getState();
const balance = selectBalance(state);
const fileInfo = makeSelectFileInfoForUri(uri)(state);
const downloadingByOutpoint = selectDownloadingByOutpoint(state);
const alreadyDownloading = fileInfo && !!downloadingByOutpoint[fileInfo.outpoint];
const alreadyStreaming = makeSelectStreamingUrlForUri(uri)(state);
if (!saveFile && (alreadyDownloading || alreadyStreaming)) {
dispatch({
type: ACTIONS.PURCHASE_URI_FAILED,
data: { uri, error: `Already fetching uri: ${uri}` },
});
if (onSuccess) {
onSuccess(fileInfo);
}
return;
}
const { cost } = costInfo;
if (parseFloat(cost) > balance) {
dispatch({
type: ACTIONS.PURCHASE_URI_FAILED,
data: { uri, error: 'Insufficient credits' },
});
Promise.resolve();
return;
}
dispatch(doFileGet(uri, saveFile, onSuccess));
};
}
export function doClearPurchasedUriSuccess() {
return {
type: ACTIONS.CLEAR_PURCHASED_URI_SUCCESS,
};
}