Skip to content

Commit

Permalink
feat: Custom sharing completed by 70%
Browse files Browse the repository at this point in the history
  • Loading branch information
1943time committed Oct 4, 2023
1 parent d03fb77 commit c33cc53
Show file tree
Hide file tree
Showing 21 changed files with 811 additions and 24 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"got": "^11.8.6",
"mime-types": "^2.1.35",
"mkdirp": "^3.0.0",
"node-ssh": "^13.1.0",
"node-watch": "^0.7.4",
"shiki": "^0.14.4",
"upath": "^2.0.1"
Expand Down
100 changes: 99 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/main/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ export const registerApi = () => {
ipcMain.handle('get-system-dark', (e) => {
return nativeTheme.shouldUseDarkColors
})
ipcMain.handle('set-service-config', (e, config: any) => {
if (!config) store.delete('service-config')
else store.set('service-config', config)
})
ipcMain.handle('get-service-config', e => {
return store.get('service-config')
})
ipcMain.on('setStore', (e, key: string, value: any) => {
if (typeof value === 'undefined') {
store.delete(key)
Expand Down
2 changes: 1 addition & 1 deletion src/main/appMenus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export const createAppMenus = () => {
}
}
]
const devTools:MenuOptions[number]['submenu'] = is.dev ? [
const devTools:MenuOptions[number]['submenu'] = is.dev || true ? [
{role: 'toggleDevTools'}
] : []
menus.push(
Expand Down
2 changes: 1 addition & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type WinOptions = {
openFolder?: string
openFile?: string
}

console.log('path', app.getPath('userData'))
const windows = new Map<number, WinOptions>()
app.setAsDefaultProtocolClient('bluestone-markdown')
function createWindow(initial?: WinOptions): void {
Expand Down
2 changes: 2 additions & 0 deletions src/preload/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {AliApi} from './sdk/ali'
import {Sdk} from './sdk'
import {Got} from 'got'
import {ExtendOptions} from 'got/dist/source/types'
import {Service} from './service'


declare global {
Expand All @@ -14,6 +15,7 @@ declare global {
api: {
sdk: typeof Sdk,
dev: boolean
service: Service,
got: Got
toUnix: (path: string) => string
md5: (str: string | Buffer) => string
Expand Down
2 changes: 2 additions & 0 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ const langSet = new Set(BUNDLED_LANGUAGES.map(l => [l.id, ...(l.aliases || [])])
let highlighter:Highlighter | null = null
import {toUnix} from 'upath'
import mime from 'mime-types'
import {Service} from './service'
let watchers = new Map<string, Watcher>()
let ready:any = null
const api = {
langSet,
service: new Service,
copyToClipboard(str: string) {
clipboard.writeText(str)
},
Expand Down
111 changes: 111 additions & 0 deletions src/preload/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import {NodeSSH} from 'node-ssh'
import {join, sep} from 'path'
import {ipcRenderer} from 'electron'
const ssh = new NodeSSH()
import {readdirSync, readFileSync} from 'fs'
import {SFTPWrapper} from 'ssh2'
export class Service {
ssh: NodeSSH | null = null
private async getAssets() {
const env = await ipcRenderer.invoke('get-env') as {webPath: string}
const files = readdirSync(env.webPath)
const scriptPath = files.find(f => f.endsWith('.js'))
const cssPath = files.find(f => f === 'style.css')
return {
script: await readFileSync(join(env.webPath, scriptPath!), {encoding:'utf-8'}),
icon: join(env.webPath, 'favicon.png'),
katex: join(env.webPath, 'katex.min.css'),
css: await readFileSync(join(env.webPath, cssPath!), {encoding:'utf-8'})
}
}
close() {
if (this.ssh) {
this.ssh.dispose()
this.ssh = null
}
}
private async getConfig() {
return ipcRenderer.invoke('get-service-config') as Record<any, any>
}
private remoteExists(sftp: SFTPWrapper, filePath: string) {
return new Promise((resolve, reject) => {
sftp.exists(filePath, (res) => {
resolve(res)
})
})
}
private async connectSsh(config: any) {
this.ssh = await ssh.connect({
host: config.host,
username: config.username,
password: config.password,
port: config.port
})
return this.ssh
}
async initialSsh(config: any) {
this.ssh = await ssh.connect({
host: config.host,
username: config.username,
password: config.password,
port: config.port
})
const assets = await this.getAssets()
await this.ssh.withSFTP(async sftp => {
const lib = join(config.target, 'lib')
const exist = await this.remoteExists(sftp, lib)
if (!exist) await this.ssh!.mkdir(lib)
await this.writeFileBySsh(sftp, join(lib, 'style.css'), assets.css)
await this.writeFileBySsh(sftp, join(lib, 'script.js'), assets.script)
})
await this.uploadFile('katex.min.css', assets.katex, 'lib')
await this.uploadFile('favicon.png', assets.icon, 'lib')
this.close()
}
private writeFileBySsh(sftp: SFTPWrapper, path: string, content: string) {
return new Promise((resolve, reject) => {
sftp.writeFile(path, content, res => {
if (res instanceof Error) {
reject(res)
} else {
resolve(null)
}
})
})
}
async uploadDoc(name: string, content: string) {
const config = await this.getConfig()
if (config.type === 'ssh') {
if (!this.ssh) this.ssh = await this.connectSsh(config)
await this.ssh.withSFTP(async sftp => {
const doc = join(config.target, 'doc')
const exist = await this.remoteExists(sftp, doc)
if (!exist) await this.ssh!.mkdir(doc)
await this.writeFileBySsh(sftp, join(config.target, name), content)
})
this.close()
}
}
async deleteDoc(path: string) {
const config = await this.getConfig()
if (config.type === 'ssh') {
if (!this.ssh) this.ssh = await this.connectSsh(config)
await this.ssh!.execCommand(`rm -f ${join(config.target, path)}`)
}
this.close()
}
async uploadFile(name: string, filePath: string, dir = 'assets'): Promise<any> {
const config = await this.getConfig()
if (config.type === 'ssh') {
if (!this.ssh) this.ssh = await this.connectSsh(config)
await this.ssh.withSFTP(async sftp => {
const assets = join(config.target, dir)
const exist = await this.remoteExists(sftp, assets)
if (!exist) await this.ssh!.mkdir(assets)
})
await this.ssh!.putFile(filePath, join(config.target, dir, name))
} else {

}
}
}
Loading

0 comments on commit c33cc53

Please sign in to comment.