@@ -12,9 +12,99 @@ import randomColor from 'randomcolor'
1212import c from 'chalk'
1313import { setTimeout as setTimeoutPromise } from 'timers/promises'
1414
15+ export async function saveSambaConfig ( ) {
16+ const { usedSpace, totalSpace } = await getStorage ( )
17+ const remainingSpace = ( totalSpace - usedSpace ) * 1024
18+ const config = await getConfig ( )
19+ const owner = await getOwner ( )
20+ let smbConfig = `# Group Name to User mapping`
21+ smbConfig += config . groups . map ( ( group ) => `# ${ group . groupName } => ${ group . usersString } \n` )
22+ smbConfig += `\n\n`
23+ smbConfig = `[global]
24+ netbios name = PIBOX
25+ workgroup = WORKGROUP
26+ access based share enum = yes
27+ logging = syslog
28+ server role = standalone server
29+ veto files = /._*/.DS_Store/
30+ delete veto files = yes
31+
32+ fruit:aapl = yes
33+ fruit:nfs_aces = no
34+ fruit:copyfile = no
35+ fruit:model = PiBox
36+ inherit permissions = yes
37+ multicast dns register = no
38+
39+ [PiBox Time Machine]
40+ vfs objects = catia fruit streams_xattr
41+ fruit:time machine = yes
42+ fruit:time machine max size = ${ remainingSpace }
43+ comment = Time Machine Backup
44+ path = /pibox/timemachine
45+ available = yes
46+ valid users = @${ owner }
47+ browseable = yes
48+ guest ok = no
49+ writable = yes
50+
51+ [Files]
52+ path = /pibox/files
53+ read only = no
54+ valid users = @${ owner }
55+ \n`
56+
57+ config . shares . forEach ( ( share ) => {
58+ smbConfig += `[${ share . name } ]
59+ path = ${ PIBOX_FILES_PREFIX + share . path }
60+ read only = no
61+ valid users = @${ share . groupName } \n\n`
62+ } )
63+
64+ await writeFile ( '/etc/samba/smb.conf' , smbConfig )
65+ await execAndLog ( 'global:samba' , 'systemctl restart smbd' )
66+ }
67+
68+ export async function ensureTimeMachine ( ) {
69+ const owner = await getOwner ( )
70+ const avahiConfig = `<?xml version="1.0" standalone='no'?>
71+ <!DOCTYPE service-group SYSTEM "avahi-service.dtd">
72+ <service-group>
73+ <name replace-wildcards="yes">%h</name>
74+ <service>
75+ <type>_smb._tcp</type>
76+ <port>445</port>
77+ </service>
78+ <service>
79+ <type>_device-info._tcp</type>
80+ <port>0</port>
81+ <txt-record>model=RackMac</txt-record>
82+ </service>
83+ <service>
84+ <type>_adisk._tcp</type>
85+ <txt-record>dk0=adVN=PiBox Time Machine,adVF=0x82</txt-record>
86+ <txt-record>sys=waMa=0,adVF=0x100</txt-record>
87+ </service>
88+ </service-group>`
89+ await writeFile ( '/etc/avahi/services/samba.service' , avahiConfig )
90+ await execAndLog ( 'global:samba' , 'systemctl restart avahi-daemon' )
91+ await execAndLog ( 'global:samba' , 'mkdir -p /pibox/timemachine' )
92+ await execAndLog ( 'global:samba' , `chown ${ owner } :${ owner } /pibox/timemachine` )
93+ }
94+
1595export const execAsync = promisify ( exec )
1696const PRESET_COLORS = '#3B89C7,#C98D09,#1BBE4D,#D8D8D4,#774399,#FF7896,#F9F871' . split ( ',' )
1797
98+ export async function getStorage ( ) {
99+ const { stdout : dfOutput } = await execAsync ( 'df /pibox' )
100+ const lines = dfOutput . split ( '\n' )
101+ const data = lines [ 1 ] . split ( / \s + / )
102+ const usedSpace = data [ 2 ]
103+ const totalSpace = data [ 1 ]
104+ const percentageUsed = parseInt ( data [ 4 ] , 10 )
105+ return { usedSpace, totalSpace, percentageUsed }
106+ }
107+
18108export async function drawHomeScreen ( ) {
19109 const start = performance . now ( )
20110 const width = 240
@@ -24,13 +114,11 @@ export async function drawHomeScreen() {
24114 const ctx = canvas . getContext ( '2d' )
25115
26116 if ( ! global . storage || global . storage . lastChecked < Date . now ( ) - 1000 * 60 * 1 ) {
27- const { stdout : dfOutput } = await execAsync ( 'df /pibox' )
28- const lines = dfOutput . split ( '\n' )
29- const data = lines [ 1 ] . split ( / \s + / )
117+ const { usedSpace, totalSpace, percentageUsed } = await getStorage ( )
30118 global . storage = { }
31- global . storage . usedSpace = data [ 2 ]
32- global . storage . totalSpace = data [ 1 ]
33- global . storage . percentageUsed = parseInt ( data [ 4 ] , 10 )
119+ global . storage . usedSpace = usedSpace
120+ global . storage . totalSpace = totalSpace
121+ global . storage . percentageUsed = percentageUsed
34122 global . storage . lastChecked = Date . now ( )
35123 }
36124
0 commit comments