lbry-desktop/src/renderer/js/redux/actions/file_info.js

148 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types from "constants/action_types";
import lbry from "lbry";
2017-11-13 22:02:23 +01:00
import { doFetchClaimListMine, doAbandonClaim } from "redux/actions/content";
2017-04-28 17:14:44 +02:00
import {
2017-05-15 05:50:59 +02:00
selectClaimsByUri,
selectIsFetchingClaimListMine,
selectMyClaimsOutpoints,
2017-11-13 22:02:23 +01:00
} from "redux/selectors/claims";
2017-05-15 05:50:59 +02:00
import {
selectIsFetchingFileList,
selectFileInfosByOutpoint,
selectUrisLoading,
selectTotalDownloadProgress,
2017-11-13 22:02:23 +01:00
} from "redux/selectors/file_info";
import { doCloseModal } from "redux/actions/app";
import { doNavigate, doHistoryBack } from "redux/actions/navigation";
import setProgressBar from "util/setProgressBar";
import batchActions from "util/batchActions";
2017-06-06 23:19:12 +02:00
const { shell } = require("electron");
2017-04-28 17:14:44 +02:00
2017-05-15 05:50:59 +02:00
export function doFetchFileInfo(uri) {
2017-04-28 17:14:44 +02:00
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
const claim = selectClaimsByUri(state)[uri];
const outpoint = claim ? `${claim.txid}:${claim.nout}` : null;
const alreadyFetching = !!selectUrisLoading(state)[uri];
2017-04-28 17:14:44 +02:00
2017-05-15 05:50:59 +02:00
if (!alreadyFetching) {
2017-04-28 17:14:44 +02:00
dispatch({
2017-05-15 05:50:59 +02:00
type: types.FETCH_FILE_INFO_STARTED,
2017-04-28 17:14:44 +02:00
data: {
2017-05-15 05:50:59 +02:00
outpoint,
2017-06-06 23:19:12 +02:00
},
});
2017-06-06 06:21:55 +02:00
2017-06-06 23:19:12 +02:00
lbry
.file_list({ outpoint: outpoint, full_status: true })
.then(fileInfos => {
dispatch({
type: types.FETCH_FILE_INFO_COMPLETED,
data: {
outpoint,
fileInfo: fileInfos && fileInfos.length ? fileInfos[0] : null,
},
});
});
2017-05-15 05:50:59 +02:00
}
2017-06-06 23:19:12 +02:00
};
2017-04-28 17:14:44 +02:00
}
export function doFileList() {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
const isFetching = selectIsFetchingFileList(state);
if (!isFetching) {
dispatch({
type: types.FILE_LIST_STARTED,
2017-06-06 23:19:12 +02:00
});
2017-06-06 23:19:12 +02:00
lbry.file_list().then(fileInfos => {
dispatch({
type: types.FILE_LIST_SUCCEEDED,
data: {
fileInfos,
2017-06-06 23:19:12 +02:00
},
});
});
}
2017-06-06 23:19:12 +02:00
};
}
export function doOpenFileInShell(path) {
return function(dispatch, getState) {
const success = shell.openItem(path);
if (!success) {
dispatch(doOpenFileInFolder(path));
}
2017-06-06 23:19:12 +02:00
};
}
export function doOpenFileInFolder(path) {
return function(dispatch, getState) {
shell.showItemInFolder(path);
2017-06-06 23:19:12 +02:00
};
}
export function doDeleteFile(outpoint, deleteFromComputer, abandonClaim) {
return function(dispatch, getState) {
const state = getState();
lbry.file_delete({
outpoint: outpoint,
delete_from_download_dir: deleteFromComputer,
});
// If the file is for a claim we published then also abandom the claim
const myClaimsOutpoints = selectMyClaimsOutpoints(state);
if (abandonClaim && myClaimsOutpoints.indexOf(outpoint) !== -1) {
const byOutpoint = selectFileInfosByOutpoint(state);
const fileInfo = byOutpoint[outpoint];
if (fileInfo) {
2017-10-24 15:10:27 +02:00
txid = fileInfo.outpoint.slice(0, -2);
nout = fileInfo.outpoint.slice(-1);
2017-11-01 21:23:30 +01:00
dispatch(doAbandonClaim(txid, nout));
}
}
dispatch({
type: types.FILE_DELETE,
data: {
2017-06-06 23:19:12 +02:00
outpoint,
},
});
const totalProgress = selectTotalDownloadProgress(getState());
setProgressBar(totalProgress);
};
}
export function doDeleteFileAndGoBack(
fileInfo,
deleteFromComputer,
abandonClaim
) {
return function(dispatch, getState) {
const actions = [];
actions.push(doCloseModal());
actions.push(doHistoryBack());
actions.push(doDeleteFile(fileInfo, deleteFromComputer, abandonClaim));
dispatch(batchActions(...actions));
2017-06-06 23:19:12 +02:00
};
}
export function doFetchFileInfosAndPublishedClaims() {
return function(dispatch, getState) {
const state = getState(),
isFetchingClaimListMine = selectIsFetchingClaimListMine(state),
isFetchingFileInfo = selectIsFetchingFileList(state);
if (!isFetchingClaimListMine) dispatch(doFetchClaimListMine());
if (!isFetchingFileInfo) dispatch(doFileList());
2017-06-06 23:19:12 +02:00
};
}