-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Manuel Valls Fernández
committed
Mar 15, 2020
0 parents
commit ea39e6b
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dist | ||
rsrc.syso |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |