Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Valls Fernández committed Mar 15, 2020
0 parents commit ea39e6b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
rsrc.syso
91 changes: 91 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"net"
"net/http"
"os"
"path/filepath"

"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/skip2/go-qrcode"
)

const size = 300

func getIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
os.Stderr.WriteString("Oops: " + err.Error() + "\n")
os.Exit(1)
}

for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil && ipnet.IP.To4()[0] == 192 {
return ipnet.IP.String()
}
}
}

return ""
}

func showError(text string) {
MainWindow{
Title: "QR Serve",
Size: Size{size + 100, 200},
Layout: VBox{},
Children: []Widget{
TextLabel{
Text: text,
TextAlignment: AlignHCenterVCenter,
},
},
}.Run()
}

func main() {
if len(os.Args) < 2 {
showError("File not specified. Please open your file with QR Serve.")
return
}

filePath := os.Args[1]
fileName := filepath.Base(filePath)
ip := getIP()

ln, err := net.Listen("tcp", ip+":0")
if err != nil {
showError(err.Error())
return
}

defer ln.Close()

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filePath)
})

go http.Serve(ln, nil)

q, _ := qrcode.New("http://"+ln.Addr().String()+"/"+fileName, qrcode.Medium)
bm, _ := walk.NewBitmapFromImage(q.Image(size))

MainWindow{
Title: fileName,
Size: Size{size + 100, size + 100},
Layout: VBox{},
Children: []Widget{
ImageView{
MinSize: Size{size, size},
MaxSize: Size{size, size},
Image: bm,
},
TextLabel{
Text: "Close this window when you're done downloading the file",
TextAlignment: AlignHCenterVCenter,
},
},
}.Run()
}
15 changes: 15 additions & 0 deletions qr-serve.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="qrserve" type="win32"/>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
</dependentAssembly>
</dependency>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True</dpiAware>
</windowsSettings>
</application>
</assembly>
4 changes: 4 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rsrc -manifest ./qr-serve.manifest
go build -ldflags="-H windowsgui"
mkdir -p dist
mv qr-serve.exe dist/

0 comments on commit ea39e6b

Please sign in to comment.