2021-10-05 16:57:47 -04:00
|
|
|
// @flow
|
2017-12-21 14:32:51 -03:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2021-10-07 23:47:39 -04:00
|
|
|
import * as ABANDON_STATES from 'constants/abandon_states';
|
2019-03-04 23:46:57 -05:00
|
|
|
// @if TARGET='app'
|
2017-12-27 20:48:11 -03:00
|
|
|
import { shell } from 'electron';
|
2019-03-04 23:46:57 -05:00
|
|
|
// @endif
|
2021-10-07 23:47:39 -04:00
|
|
|
import Lbry from 'lbry';
|
2021-12-31 12:52:26 -05:00
|
|
|
import { selectClaimForUri } from 'redux/selectors/claims';
|
2021-10-07 23:47:39 -04:00
|
|
|
import { doAbandonClaim } from 'redux/actions/claims';
|
|
|
|
import { batchActions } from 'util/batch-actions';
|
|
|
|
|
2018-10-29 13:23:53 -04:00
|
|
|
import { doHideModal } from 'redux/actions/app';
|
2019-04-19 12:55:21 -04:00
|
|
|
import { goBack } from 'connected-react-router';
|
2019-08-13 23:56:11 -04:00
|
|
|
import { doSetPlayingUri } from 'redux/actions/content';
|
2019-08-14 22:50:41 -04:00
|
|
|
import { selectPlayingUri } from 'redux/selectors/content';
|
2021-10-05 16:57:47 -04:00
|
|
|
import { doToast } from 'redux/actions/notifications';
|
2021-10-07 23:47:39 -04:00
|
|
|
import { selectBalance } from 'redux/selectors/wallet';
|
|
|
|
import {
|
|
|
|
makeSelectFileInfoForUri,
|
|
|
|
selectDownloadingByOutpoint,
|
|
|
|
makeSelectStreamingUrlForUri,
|
|
|
|
} from 'redux/selectors/file_info';
|
|
|
|
|
2021-10-05 16:57:47 -04:00
|
|
|
type Dispatch = (action: any) => any;
|
|
|
|
type GetState = () => { file: FileState };
|
|
|
|
export function doOpenFileInFolder(path: string) {
|
2017-12-27 20:48:11 -03:00
|
|
|
return () => {
|
2017-12-21 14:32:51 -03:00
|
|
|
shell.showItemInFolder(path);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-05 16:57:47 -04:00
|
|
|
export function doOpenFileInShell(path: string) {
|
|
|
|
return (dispatch: Dispatch) => {
|
2020-10-15 19:06:05 -04:00
|
|
|
const success = shell.openPath(path);
|
2017-08-06 19:18:38 -04:00
|
|
|
if (!success) {
|
2017-09-20 08:47:08 -04:00
|
|
|
dispatch(doOpenFileInFolder(path));
|
2017-08-06 19:18:38 -04:00
|
|
|
}
|
2017-06-06 17:19:12 -04:00
|
|
|
};
|
2017-04-30 00:02:25 +07:00
|
|
|
}
|
|
|
|
|
2021-10-05 16:57:47 -04:00
|
|
|
export function doDeleteFile(outpoint: string, deleteFromComputer?: boolean, abandonClaim?: boolean, cb: any) {
|
|
|
|
return (dispatch: Dispatch) => {
|
2020-07-22 14:22:32 -04:00
|
|
|
if (abandonClaim) {
|
2019-05-10 02:27:51 -04:00
|
|
|
const [txid, nout] = outpoint.split(':');
|
2020-07-22 18:39:24 -04:00
|
|
|
dispatch(doAbandonClaim(txid, Number(nout), cb));
|
2017-06-29 14:44:34 +07:00
|
|
|
}
|
2020-07-22 14:22:32 -04:00
|
|
|
|
2020-03-27 12:49:41 -04:00
|
|
|
// @if TARGET='app'
|
2020-04-27 18:22:09 -04:00
|
|
|
Lbry.file_delete({
|
|
|
|
outpoint,
|
|
|
|
delete_from_download_dir: deleteFromComputer,
|
|
|
|
});
|
2020-07-22 14:22:32 -04:00
|
|
|
|
2017-04-30 00:02:25 +07:00
|
|
|
dispatch({
|
2017-12-21 14:32:51 -03:00
|
|
|
type: ACTIONS.FILE_DELETE,
|
2017-04-30 00:02:25 +07:00
|
|
|
data: {
|
2017-06-06 17:19:12 -04:00
|
|
|
outpoint,
|
|
|
|
},
|
|
|
|
});
|
2020-03-27 12:49:41 -04:00
|
|
|
// @endif
|
2017-07-21 15:13:45 +07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-05 16:57:47 -04:00
|
|
|
export function doDeleteFileAndMaybeGoBack(
|
|
|
|
uri: string,
|
|
|
|
deleteFromComputer?: boolean,
|
|
|
|
abandonClaim?: boolean,
|
|
|
|
doGoBack: (any) => void
|
|
|
|
) {
|
|
|
|
return (dispatch: Dispatch, getState: GetState) => {
|
2019-08-14 22:50:41 -04:00
|
|
|
const state = getState();
|
|
|
|
const playingUri = selectPlayingUri(state);
|
2019-08-15 07:36:03 -04:00
|
|
|
const { outpoint } = makeSelectFileInfoForUri(uri)(state) || '';
|
2021-12-31 12:52:26 -05:00
|
|
|
const { nout, txid } = selectClaimForUri(state, uri);
|
2019-10-01 09:44:37 -04:00
|
|
|
const claimOutpoint = `${txid}:${nout}`;
|
2017-07-21 15:13:45 +07:00
|
|
|
const actions = [];
|
2020-07-22 18:39:24 -04:00
|
|
|
|
|
|
|
if (!abandonClaim) {
|
|
|
|
actions.push(doHideModal());
|
|
|
|
}
|
|
|
|
|
|
|
|
actions.push(
|
2021-02-25 14:23:23 +08:00
|
|
|
doDeleteFile(outpoint || claimOutpoint, deleteFromComputer, abandonClaim, (abandonState) => {
|
2020-07-22 18:39:24 -04:00
|
|
|
if (abandonState === ABANDON_STATES.DONE) {
|
|
|
|
if (abandonClaim) {
|
2021-06-24 11:33:11 -03:00
|
|
|
if (doGoBack) {
|
|
|
|
dispatch(goBack());
|
|
|
|
}
|
2020-07-22 18:39:24 -04:00
|
|
|
dispatch(doHideModal());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2019-08-14 22:50:41 -04:00
|
|
|
|
2021-02-25 14:41:34 +08:00
|
|
|
if (playingUri && playingUri.uri === uri) {
|
2020-12-21 10:31:34 -05:00
|
|
|
actions.push(doSetPlayingUri({ uri: null }));
|
2019-08-14 22:50:41 -04:00
|
|
|
}
|
2019-08-30 10:01:39 -04: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
|
2019-08-14 22:50:41 -04:00
|
|
|
|
|
|
|
dispatch(batchActions(...actions));
|
2017-06-06 17:19:12 -04:00
|
|
|
};
|
2017-04-30 23:01:43 +07:00
|
|
|
}
|
2021-10-05 16:57:47 -04:00
|
|
|
|
|
|
|
export function doFileGet(uri: string, saveFile: boolean = true, onSuccess?: (GetResponse) => any) {
|
|
|
|
return (dispatch: Dispatch, getState: () => any) => {
|
|
|
|
const state = getState();
|
2021-12-31 12:52:26 -05:00
|
|
|
const { nout, txid } = selectClaimForUri(state, uri);
|
2021-10-05 16:57:47 -04: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,
|
|
|
|
};
|
|
|
|
}
|