forked from onblog/BlogHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-localFile.js
More file actions
67 lines (64 loc) · 2.45 KB
/
app-localFile.js
File metadata and controls
67 lines (64 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const {dialog} = require('electron')
const util = require('./app-util')
const fs = require('fs')
const path = require('path')
// 读取选中的多个文件信息
exports.openManyLocalFile = (callback) => {
dialog.showOpenDialog({
properties: ['openFile', 'createDirectory', 'multiSelections'],
filters: [
{name: 'markdown', extensions: ['md']}
]
})
.then(files => {
if (!files.canceled) {
for (let i = 0; i < files.filePaths.length; i++) {
const filePath = files.filePaths[i]
const title = util.getTitle(filePath)
const dirname = path.dirname(filePath)
fs.readFile(filePath, function (err, data) {
if (err) {
return console.error(err);
}
const content = data.toString()
callback(title, content, dirname)
});
}
}
})
.catch(err => {
console.log(err)
})
}
// 读取选中的多个文件信息(同步)
exports.openManyLocalFileSync = (filters) => {
let files = dialog.showOpenDialogSync({
properties: ['openFile', 'createDirectory',
'multiSelections'],
filters: filters ? filters :[
{name: 'markdown', extensions: ['md']}
]
})
let result = {}
result.canceled = (files === undefined)
if (files) {
result.files = []
for (let i = 0; i < files.length; i++) {
// 已选中文件
const filepath = files[i]
const title = util.getTitle(filepath)
const dirname = path.dirname(filepath)
const content = fs.readFileSync(filepath)
const extname = path.extname(filepath)
// 返回
result.files[i] = {
filepath: filepath,
title: title,
content: content.toString(),
dirname: dirname,
extname: extname
}
}
}
return result
}