2020-07-05 00:24:34 +03:00
|
|
|
import * as Constants from './constants';
|
2020-06-17 21:05:13 +03:00
|
|
|
|
2020-07-03 18:32:36 +03:00
|
|
|
import FS from 'fs-extra';
|
2020-06-17 21:05:13 +03:00
|
|
|
|
|
|
|
export const resetFileSystem = async () => {
|
2020-07-03 18:32:36 +03:00
|
|
|
console.log('[ prototype ] deleting old token and library data ');
|
2020-06-17 21:05:13 +03:00
|
|
|
if (FS.existsSync(`./.data`)) {
|
2020-07-03 18:32:36 +03:00
|
|
|
FS.removeSync('./.data', { recursive: true });
|
2020-06-17 21:05:13 +03:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:32:36 +03:00
|
|
|
console.log('[ prototype ] deleting old avatar data ');
|
2020-06-17 21:05:13 +03:00
|
|
|
if (FS.existsSync(Constants.AVATAR_STORAGE_URL)) {
|
|
|
|
FS.removeSync(Constants.AVATAR_STORAGE_URL, { recursive: true });
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:32:36 +03:00
|
|
|
console.log('[ prototype ] deleting old file data ');
|
2020-06-17 21:05:13 +03:00
|
|
|
if (FS.existsSync(Constants.FILE_STORAGE_URL)) {
|
|
|
|
FS.removeSync(Constants.FILE_STORAGE_URL, { recursive: true });
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:32:36 +03:00
|
|
|
console.log('[ prototype ] creating new avatar folder ');
|
2020-06-17 21:05:13 +03:00
|
|
|
FS.mkdirSync(Constants.AVATAR_STORAGE_URL, { recursive: true });
|
2020-07-03 18:32:36 +03:00
|
|
|
FS.writeFileSync(`${Constants.AVATAR_STORAGE_URL}.gitkeep`, '');
|
2020-06-17 21:05:13 +03:00
|
|
|
|
2020-07-03 18:32:36 +03:00
|
|
|
console.log('[ prototype ] creating new local file folder ');
|
2020-06-17 21:05:13 +03:00
|
|
|
FS.mkdirSync(Constants.FILE_STORAGE_URL, { recursive: true });
|
2020-07-03 18:32:36 +03:00
|
|
|
FS.writeFileSync(`${Constants.FILE_STORAGE_URL}.gitkeep`, '');
|
2020-06-17 21:05:13 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// NOTE(jim): Data that does not require a Powergate token.
|
|
|
|
export const refresh = async ({ PG }) => {
|
|
|
|
const Health = await PG.health.check();
|
|
|
|
const status = Health.status ? Health.status : null;
|
|
|
|
const messageList = Health.messageList ? Health.messageList : null;
|
|
|
|
|
|
|
|
const Peers = await PG.net.peers();
|
|
|
|
const peersList = Peers.peersList ? Peers.peersList : null;
|
|
|
|
|
|
|
|
return { peersList, messageList, status };
|
|
|
|
};
|
|
|
|
|
|
|
|
// NOTE(jim): Data that does require a powergate token.
|
|
|
|
export const refreshWithToken = async ({ PG }) => {
|
|
|
|
const Addresses = await PG.ffs.addrs();
|
|
|
|
const addrsList = Addresses.addrsList ? Addresses.addrsList : null;
|
|
|
|
|
|
|
|
const NetworkInfo = await PG.ffs.info();
|
|
|
|
const info = NetworkInfo.info ? NetworkInfo.info : null;
|
|
|
|
|
|
|
|
return { addrsList, info };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const emitState = async ({ state, client, PG }) => {
|
|
|
|
const { peersList, messageList, status } = await refresh({ PG });
|
|
|
|
const { addrsList, info } = await refreshWithToken({ PG });
|
|
|
|
|
|
|
|
const data = await updateStateData(state, {
|
|
|
|
peersList,
|
|
|
|
messageList,
|
|
|
|
status,
|
|
|
|
addrsList,
|
|
|
|
info,
|
|
|
|
state,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (client) {
|
2020-07-03 18:32:36 +03:00
|
|
|
client.send(JSON.stringify({ action: 'UPDATE_VIEWER', data }));
|
2020-06-17 21:05:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getFileName = (s) => {
|
|
|
|
let target = s;
|
2020-07-03 18:32:36 +03:00
|
|
|
if (target.endsWith('/')) {
|
2020-06-17 21:05:13 +03:00
|
|
|
target = target.substring(0, target.length - 1);
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:32:36 +03:00
|
|
|
return target.substr(target.lastIndexOf('/') + 1);
|
2020-06-17 21:05:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const createFile = ({ id, data }) => {
|
|
|
|
return {
|
2020-07-03 18:32:36 +03:00
|
|
|
decorator: 'FILE',
|
2020-06-17 21:05:13 +03:00
|
|
|
id: id,
|
2020-07-03 18:32:36 +03:00
|
|
|
icon: 'PNG',
|
2020-06-17 21:05:13 +03:00
|
|
|
file: getFileName(id),
|
|
|
|
miner: null,
|
|
|
|
job_id: null,
|
|
|
|
cid: null,
|
|
|
|
date: new Date(),
|
|
|
|
size: data.size,
|
|
|
|
amount: 0,
|
|
|
|
remaining: null,
|
|
|
|
deal_category: 1,
|
|
|
|
retrieval_status: 0,
|
|
|
|
storage_status: 0,
|
|
|
|
errors: [],
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const createFolder = ({ id }) => {
|
|
|
|
return {
|
2020-07-03 18:32:36 +03:00
|
|
|
decorator: 'FOLDER',
|
2020-06-17 21:05:13 +03:00
|
|
|
id,
|
|
|
|
folderId: id,
|
2020-07-03 18:32:36 +03:00
|
|
|
icon: 'FOLDER',
|
2020-06-17 21:05:13 +03:00
|
|
|
file: getFileName(id),
|
|
|
|
name: getFileName(id),
|
|
|
|
pageTitle: null,
|
|
|
|
date: null,
|
|
|
|
size: null,
|
|
|
|
children: [],
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateStateData = async (state, newState) => {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...newState,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO(jim): Refactor this so we repeat this less often.
|
|
|
|
export const refreshLibrary = async ({ state, PG, FFS }) => {
|
|
|
|
let write = false;
|
|
|
|
for (let i = 0; i < state.library.length; i++) {
|
|
|
|
for (let j = 0; j < state.library[i].children.length; j++) {
|
|
|
|
if (state.library[i].children[j].job_id) {
|
|
|
|
if (state.library[i].children[j].storage_status === 1) {
|
2020-07-02 11:59:50 +03:00
|
|
|
console.log(
|
2020-07-03 18:32:36 +03:00
|
|
|
'[ prototype ] update file',
|
2020-07-02 11:59:50 +03:00
|
|
|
state.library[i].children[j]
|
|
|
|
);
|
2020-06-17 21:05:13 +03:00
|
|
|
state.library[i].children[j].storage_status = 2;
|
|
|
|
write = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PG.ffs.watchJobs((job) => {
|
2020-07-03 18:32:36 +03:00
|
|
|
console.log('[ prototype ] job status', job.status);
|
|
|
|
if (job.status === FFS.JobStatus.JOB_STATUS_SUCCESS) {
|
2020-07-02 11:59:50 +03:00
|
|
|
console.log(
|
2020-07-03 18:32:36 +03:00
|
|
|
'[ prototype ] update file',
|
2020-07-02 11:59:50 +03:00
|
|
|
state.library[i].children[j]
|
|
|
|
);
|
2020-06-17 21:05:13 +03:00
|
|
|
state.library[i].children[j].storage_status = 6;
|
|
|
|
write = true;
|
|
|
|
}
|
|
|
|
}, state.library[i].children[j].job_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write) {
|
2020-07-02 11:59:50 +03:00
|
|
|
FS.writeFileSync(
|
2020-07-03 18:32:36 +03:00
|
|
|
'./.data/library.json',
|
2020-07-02 11:59:50 +03:00
|
|
|
JSON.stringify({ library: state.library })
|
|
|
|
);
|
2020-06-17 21:05:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return { ...state };
|
|
|
|
};
|