-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
154 lines (126 loc) · 4.09 KB
/
main.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const { menubar } = require('menubar')
const http = require('http')
const micro = require('micro')
const { createError } = require('micro')
const path = require('path')
const fs = require('fs')
const internalip = require('internal-ip')
const AutoLaunch = require('auto-launch')
const { clipboard, Menu } = require('electron')
const PORT = 4500
let mb = menubar()
let files = []
let ip = internalip.v4.sync()
let staticFiles = [
'/build/script.js',
'/build/style.css',
]
function page(contents) {
return `<html>
<head>
<title>drop - local file sharing</title>
<link rel="stylesheet" href="./build/style.css">
</head>
<body>
${contents}
<div class=footer>
<a href="http://${ip}:${PORT}" class=ip>${ip}:${PORT}</a>
</div>
<div class="credit">by cesque :)</div>
</body>
<script>
console.log(${JSON.stringify(files)})
</script>
<script src="./build/script.js"></script>
</html>`
}
mb.on('ready', async () => {
console.log('ready')
var autoLauncher = new AutoLaunch({
name: 'Drop',
})
const server = new http.Server(micro(async (req, res) => {
if(req.url == '/') {
if(files.length > 0) {
let urls = files.map(file => {
let filename = path.basename(file.path)
return `<a class="file" href="/${filename}" data-time-left="${Math.floor(file.timeout)}">
${filename} <span class="info">- <span class="time">${Math.floor(file.timeout)}</span></span>
</a><br>`
})
return page(urls.join(''))
} else {
return page(`<p class=no-files>no files uploaded yet :)</p>`)
}
} else if (staticFiles.includes(req.url)) {
let parts = req.url.split('/')
let last = parts[parts.length - 1]
return fs.readFileSync(__dirname + '/build/' + last)
} else {
let file = files.find(x => ('/' + x.filename) == req.url)
if(!file) throw micro.createError(
404,
`file not found for url "${req.url}" :(`
)
return fs.readFileSync(file.path)
}
}))
server.listen(PORT)
mb.tray.removeAllListeners('click')
mb.tray.setTitle('drop')
mb.tray.setImage(__dirname + '/build/icon.png')
let isAutoLaunch = await autoLauncher.isEnabled()
let contextMenu = Menu.buildFromTemplate([
{
label: 'Copy URL',
click: () => {
clipboard.writeText('http://' + ip + ':' + PORT)
},
},
{
label: 'Launch at login',
type: 'checkbox',
checked: isAutoLaunch,
click: (menuItem, browserWindow, event) => {
if(menuItem.checked) {
autoLauncher.enable()
} else {
autoLauncher.disable()
}
}
},
{
type: 'separator'
},
{
label: 'Quit',
role: 'quit',
}
])
mb.tray.setContextMenu(contextMenu)
mb.tray.on('drop-files', (event, dropped) => {
let droppedInfo = dropped.map(file => {
return {
path: file,
timeout: 180,
filename: encodeURIComponent(path.basename(file)),
}
})
files.push(...droppedInfo)
mb.tray.setImage(__dirname + '/build/icon-full.png')
let lastFileURI = droppedInfo[droppedInfo.length - 1].filename;
clipboard.writeText('http://' + ip + ':' + PORT + '/' + lastFileURI)
})
mb.app.on('before-quit', () => {
server.close()
})
setInterval(() => {
for(let file of files) {
file.timeout -= 1
}
files = files.filter(x => x.timeout > 0)
if(files.length == 0) {
mb.tray.setImage(__dirname + '/build/icon.png')
}
}, 1000)
})