Small things

This commit is contained in:
Jiiks 2018-08-30 09:08:52 +03:00
parent ecb0b3e9d0
commit 6db016060b
5 changed files with 57 additions and 52 deletions

View File

@ -11,7 +11,7 @@ import Security from './security';
export default class {
static async installPackageEvent(pkg, upload) {
static async installPackageDragAndDrop(pkg, upload) {
try {
const config = JSON.parse(asar.extractFile(pkg.path, 'config.json').toString());
const { info, main } = config;
@ -21,54 +21,31 @@ export default class {
const extractIcon = asar.extractFile(pkg.path, info.icon);
icon = `data:${info.icon_type};base64,${Utils.arrayBufferToBase64(extractIcon)}`;
}
if (icon) config.iconEncoded = icon;
const isPlugin = info.type && info.type === 'plugin' || main.endsWith('.js');
config.path = pkg.path;
/*
config.permissions = [
{
HEADER: 'Test Permission Header',
BODY: 'Test Permission Body'
},
{
HEADER: 'Test Permission Header',
BODY: 'Test Permission Body'
},
{
HEADER: 'Test Permission Header',
BODY: 'Test Permission Body'
},
{
HEADER: 'Test Permission Header',
BODY: 'Test Permission Body'
}
];
*/
// Show install modal
const modalResult = await Modals.installModal(isPlugin ? 'plugin' : 'theme', config).promise;
const modalResult = await Modals.installModal(isPlugin ? 'plugin' : 'theme', config, pkg.path, icon).promise;
if (modalResult === 0) {
// Upload it instead
}
console.log(modalResult);
} catch (err) {}
}
/**
* Hash and verify a package
* @param {Byte[]|String} bytesOrPath byte array of binary or path to local file
* @param {String} id Package id
*/
static async verifyPackage(bytesOrPath) {
static async verifyPackage(bytesOrPath, id) {
const bytes = typeof bytesOrPath === 'string' ? fs.readFileSync(bytesOrPath) : bytesOrPath;
// Temporary hash to simulate response from server
const tempVerified = '2e3532ee366816adc37b0f478bfef35e03f96e7aeee9b115f5918ef6a4e94de8';
const tempVerified = ['2e3532ee366816adc37b0f478bfef35e03f96e7aeee9b115f5918ef6a4e94de8', '06a2eb4e37b926354ab80cd83207db67e544c932e9beddce545967a21f8db5aa'];
const hashBytes = Security.hash('sha256', bytes, 'hex');
return hashBytes === tempVerified;
return tempVerified.includes(hashBytes);
}
// TODO lots of stuff
@ -78,17 +55,18 @@ export default class {
* @param {String} name Package name
* @param {Boolean} update Does an older version already exist
*/
static async installPackage(bytesOrPath, name, update = false) {
static async installPackage(bytesOrPath, id, update = false) {
const bytes = typeof bytesOrPath === 'string' ? fs.readFileSync(bytesOrPath) : bytesOrPath;
const outputPath = path.join(Globals.getPath('plugins'), `${name}.bd`);
const outputName = `${id}.bd`;
const outputPath = path.join(Globals.getPath('plugins'), outputName);
fs.writeFileSync(outputPath, bytes);
if (!update) {
return PluginManager.preloadPackedContent(`${name}.bd`);
return PluginManager.preloadPackedContent(outputName);
}
return PluginManager.reloadContent(PluginManager.getPluginByName(name));
return PluginManager.reloadContent(PluginManager.getPluginById(id));
}
}

View File

@ -514,7 +514,7 @@ export class ReactAutoPatcher {
e.stopImmediatePropagation();
stateNode.clearDragging();
PackageInstaller.installPackageEvent(e.dataTransfer.files[0], DiscordApi.currentChannel.id);
PackageInstaller.installPackageDragAndDrop(e.dataTransfer.files[0], DiscordApi.currentChannel.id);
// Events.emit('install-pkg', e.dataTransfer.files[0], DiscordApi.currentChannel.id);
};

View File

@ -1,5 +1,11 @@
.bd-installModal {
&.bd-err {
.bd-modalInner {
border: 2px solid $colerr;
}
}
.bd-modalInner {
min-width: 520px;
min-height: 200px;
@ -85,7 +91,17 @@
background: rgba(0, 0, 0, .1);
.bd-installModalStatus {
color: #fff;
height: 36px;
line-height: 36px;
flex-grow: 1;
padding: 0 25px;
}
.bd-installModalStatus {
&:not(.bd-err) {
color: #fff;
}
font-weight: 600;
}
@ -94,14 +110,21 @@
padding: 10px;
border-radius: 3px;
min-width: 100px;
background: #3ecc9c;
transition: opacity .2s ease-in-out;
&.bd-ok {
background: #3ecc9c;
}
&.bd-err {
color: #fff;
}
&:hover {
opacity: .8;
}
&:first-child {
&.bd-installModalUpload {
background: transparent;
transition: none;

View File

@ -1,13 +1,13 @@
<template>
<Modal class="bd-installModal" :headertext="modal.title" :closing="modal.closing" @close="modal.close" :noheader="true">
<Modal class="bd-installModal" :headertext="modal.title" :closing="modal.closing" @close="modal.close" :noheader="true" :class="{'bd-err': !verifying && !verified}">
<div slot="body" class="bd-installModalBody">
<div class="bd-installModalTop">
<div class="bd-installModalIcon">
<div v-if="modal.config.iconEncoded" class="bd-installModalCi" :style="{backgroundImage: `url(${modal.config.iconEncoded})`}"/>
<div v-if="modal.icon" class="bd-installModalCi" :style="{backgroundImage: `url(${modal.icon})`}"/>
<MiExtension v-else/>
</div>
<div class="bd-installModalInfo">
<span>{{modal.config.info.name}} v{{modal.config.info.version.toString()}} by {{modal.config.info.authors.map(a => a.name).join(', ')}}</span>
<span>{{modal.config.info.name}} v{{modal.config.info.version}} by {{modal.config.info.authors.map(a => a.name).join(', ')}}</span>
<div class="bd-installModalDesc">
{{modal.config.info.description}}
</div>
@ -28,16 +28,19 @@
</div>
</div>
<div v-if="verifying" slot="footer" class="bd-installModalFooter">
<span class="bd-installModalStatus">Verifying {{this.modal.contentType}}</span>
<span class="bd-installModalStatus">Verifying {{modal.contentType}}</span>
</div>
<div v-else-if="!verified" slot="footer" class="bd-installModalFooter">
<span class="bd-installModalStatus">Not verified!</span>
<span class="bd-installModalStatus bd-err">Not verified!</span>
<div class="bd-button bd-installModalUpload" @click="modal.confirm(0); modal.close();">Upload</div>
<div class="bd-button bd-err" @click="install" v-if="allowUnsafe">{{ !alreadyInstalled ? 'Install' : 'Update' }}</div>
</div>
<div v-else-if="alreadyInstalled && upToDate" slot="footer" class="bd-installModalFooter">
<span class="bd-installModalStatus">Up to date version already installed!</span>
<div class="bd-button bd-installModalUpload" @click="modal.confirm(0); modal.close();">Upload</div>
</div>
<div v-else slot="footer" class="bd-installModalFooter">
<div class="bd-button bd-ok" @click="modal.confirm(0); modal.close();">Upload</div>
<div class="bd-button bd-installModalUpload" @click="modal.confirm(0); modal.close();">Upload</div>
<div class="bd-button bd-ok" @click="install">{{ !alreadyInstalled ? 'Install' : 'Update' }}</div>
</div>
</Modal>
@ -45,7 +48,7 @@
<script>
// Imports
import { Modal, MiExtension } from '../../common';
import { PluginManager, ThemeManager, PackageInstaller } from 'modules';
import { PluginManager, ThemeManager, PackageInstaller, Settings } from 'modules';
export default {
data() {
@ -53,7 +56,8 @@
installing: false,
verifying: true,
alreadyInstalled: false,
upToDate: true
upToDate: true,
allowUnsafe: Settings.getSetting('security', 'default', 'unsafe-content').value
}
},
props: ['modal'],
@ -74,12 +78,12 @@
},
methods: {
async verify() {
const verified = await PackageInstaller.verifyPackage(this.modal.config.path);
const verified = await PackageInstaller.verifyPackage(this.modal.filePath);
this.verified = verified;
this.verifying = false;
},
async install() {
const installed = await PackageInstaller.installPackage(this.modal.config.path, this.modal.config.info.name, this.alreadyInstalled);
const installed = await PackageInstaller.installPackage(this.modal.filePath, this.modal.config.info.id, this.alreadyInstalled);
console.log(installed);
this.modal.confirm();
this.modal.close();

View File

@ -191,12 +191,12 @@ export default class Modals {
return new Modal(modal, InputModal);
}
static installModal(contentType, config) {
return this.add(this.createInstallModal(contentType, config));
static installModal(contentType, config, filePath, icon) {
return this.add(this.createInstallModal(contentType, config, filePath, icon));
}
static createInstallModal(contentType, config) {
const modal = { contentType, config };
static createInstallModal(contentType, config, filePath, icon) {
const modal = { contentType, config, filePath, icon };
modal.promise = new Promise((resolve, reject) => {
modal.confirm = value => resolve(value);
modal.beforeClose = () => reject();