Skip to content

Commit

Permalink
Merge pull request #6 from mrf345/testing
Browse files Browse the repository at this point in the history
Add open .sla files support, and refactor title updates
  • Loading branch information
mrf345 authored Sep 10, 2024
2 parents 559b42f + 2b7c8d9 commit 877c338
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ jobs:
- name: Build Windows NSIS installer (Normal)
if: runner.os == 'Windows' && startsWith(matrix.build.tag, 'windows-a')
shell: bash
run: wails build -platform ${{ matrix.build.platform }} -nsis -windowsconsole -ldflags "-X main.version=v${{ steps.normalize_version.outputs.version }}"
run: wails build -platform ${{ matrix.build.platform }} -nsis -ldflags "-X main.version=v${{ steps.normalize_version.outputs.version }}"

- name: Build Windows NSIS installer (Portable)
if: runner.os == 'Windows' && startsWith(matrix.build.tag, 'windows-portable')
shell: bash
run: wails build -platform ${{ matrix.build.platform }} -nsis -ldflags "-X main.version=v${{ steps.normalize_version.outputs.version }} -X main.portablebuild=true" -windowsconsole
run: wails build -platform ${{ matrix.build.platform }} -nsis -ldflags "-X main.version=v${{ steps.normalize_version.outputs.version }} -X main.portablebuild=true"

# Packaging

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
v ?= 1.0.0
v ?= 1.0.1

pkg-some:
wails build -platform windows/amd64,windows/arm64,linux/amd64
Expand Down
9 changes: 9 additions & 0 deletions backend/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package backend

import (
"embed"
"os"
"path/filepath"
"runtime"

desktopEntry "github.com/mrf345/desktop-entry"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
"github.com/wailsapp/wails/v2/pkg/options/linux"
"github.com/wailsapp/wails/v2/pkg/options/mac"
)

func NewApp(icon []byte, assets embed.FS) (*App, *options.App) {
Expand Down Expand Up @@ -35,6 +38,7 @@ func NewApp(icon []byte, assets embed.FS) (*App, *options.App) {
AssetServer: &assetserver.Options{Assets: assets},
Bind: []interface{}{app},
Linux: &linux.Options{Icon: icon},
Mac: &mac.Options{OnFileOpen: app.openFileForMac},
DragAndDrop: &options.DragAndDrop{
EnableFileDrop: true,
},
Expand All @@ -45,5 +49,10 @@ func NewDesktopEntry(icon []byte) *desktopEntry.DesktopEntry {
entry := desktopEntry.New(Name, Version, icon)
entry.Comment = "Fast & simple drag & drop files encryption tool"
entry.Categories = "Utility;Security;"
entry.MimeType.Path = filepath.Join(os.Getenv("HOME"), ".local/share/mime")
entry.MimeType.Type = "application/x-safelock"
entry.MimeType.GenericIcon = "package-x-generic"
entry.MimeType.Comment = "Safelock encrypted file"
entry.MimeType.Patterns = []string{"*.sla"}
return entry
}
44 changes: 31 additions & 13 deletions backend/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"context"
"fmt"
"os"
"strings"
"time"
Expand All @@ -11,27 +12,34 @@ import (
)

const (
Version = "1.0.0"
Name = "Safelock"
statusUpdateKey = "status_update"
statusEndKey = "status_end"
openedSlaKey = "opened_sla_file"
kindEncrypt = "encrypt"
kindDecrypt = "decrypt"
Version = "1.0.1"
Name = "Safelock"
statusUpdateKey = "status_update"
statusEndKey = "status_end"
openedSlaKey = "opened_sla_file"
kindEncrypt taskKind = "encrypt"
kindDecrypt taskKind = "decrypt"
)

type taskKind string

func (tk taskKind) Str() string {
return string(tk)
}

var (
MessageDialog = runtime.MessageDialog
SaveFileDialog = runtime.SaveFileDialog
OpenDirectoryDialog = runtime.OpenDirectoryDialog
EventsEmit = runtime.EventsEmit
WindowSetTitle = runtime.WindowSetTitle
)

type Task struct {
id string
status string
percent float64
kind string
kind taskKind
lock *safelock.Safelock
cancel context.CancelFunc
}
Expand All @@ -45,7 +53,7 @@ func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}

func (a *App) domReady(ctx context.Context) {
func (a App) domReady(ctx context.Context) {
runtime.WindowCenter(ctx)

isSlaFileOpened := len(os.Args) > 1 && strings.HasSuffix(os.Args[1], ".sla")
Expand All @@ -59,27 +67,37 @@ func (a *App) domReady(ctx context.Context) {
}
}

func (a *App) GetVersion() string {
func (a App) openFileForMac(path string) {
if !strings.HasSuffix(path, ".sla") {
a.ShowErrMsg(fmt.Sprintf("Unsupported file format (%s)", path))
return
}

EventsEmit(a.ctx, openedSlaKey, path)
runtime.WindowShow(a.ctx)
}

func (a App) GetVersion() string {
return Version
}

func (a *App) ShowErrMsg(msg string) {
func (a App) ShowErrMsg(msg string) {
_, _ = MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "😞 Failure",
Message: msg,
})
}

func (a *App) ShowInfoMsg(msg string) {
func (a App) ShowInfoMsg(msg string) {
_, _ = MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "🎉 Success",
Message: msg,
})
}

func (a *App) Cancel() {
func (a App) Cancel() {
if len(a.task.id) > 0 {
a.task.cancel()
}
Expand Down
1 change: 1 addition & 0 deletions backend/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (a *App) Decrypt(path string, password string) (id string, err error) {
}

inputFile.Close()
WindowSetTitle(a.ctx, Name)
}()

return
Expand Down
2 changes: 2 additions & 0 deletions backend/decrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestDecrypt(t *testing.T) {

mockOpenDirectoryDialog(tempDir, nil)
mockEventsEmit()
mockWindowSetTitle()
dTypeChan := mockMessageDialog()
_, err := app.Decrypt(outputPath, pwd)
dialogType := <-dTypeChan
Expand All @@ -55,6 +56,7 @@ func TestDecryptFail(t *testing.T) {

mockOpenDirectoryDialog(tempDir, nil)
mockEventsEmit()
mockWindowSetTitle()
dTypeChan := mockMessageDialog()
_, err := app.Decrypt(outputPath, "wrong pass")
dialogType := <-dTypeChan
Expand Down
6 changes: 5 additions & 1 deletion backend/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func (a *App) Encrypt(paths []string, password string) (id string, err error) {
return "", err
}

if outputFile, err = os.OpenFile(outputPath, os.O_RDWR|os.O_CREATE, 0755); err != nil {
fileFlags := os.O_RDWR | os.O_CREATE | os.O_TRUNC

if outputFile, err = os.OpenFile(outputPath, fileFlags, 0755); err != nil {
a.ShowErrMsg(fmt.Sprintf("Failure: %s", err.Error()))
cancel()
return
Expand All @@ -62,6 +64,8 @@ func (a *App) Encrypt(paths []string, password string) (id string, err error) {
outputFile.Close()
_ = os.Remove(outputFile.Name())
}

WindowSetTitle(a.ctx, Name)
}()

return
Expand Down
10 changes: 10 additions & 0 deletions backend/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestEncrypt(t *testing.T) {

mockSaveFileDialog(outputPath, nil)
mockEventsEmit()
mockWindowSetTitle()
dTypeChan := mockMessageDialog()
_, err := app.Encrypt(inputs, pwd)
dialogType := <-dTypeChan
Expand All @@ -59,6 +60,7 @@ func TestEncryptFail(t *testing.T) {

mockSaveFileDialog(outputPath, nil)
mockEventsEmit()
mockWindowSetTitle()
dTypeChan := mockMessageDialog()
_, err := app.Encrypt(inputs, pwd)
dialogType := <-dTypeChan
Expand Down Expand Up @@ -95,3 +97,11 @@ func mockEventsEmit() chan string {
}
return event
}

func mockWindowSetTitle() chan string {
titles := make(chan string, 10000)
WindowSetTitle = func(ctx context.Context, title string) {
titles <- title
}
return titles
}
11 changes: 10 additions & 1 deletion backend/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"fmt"
"strings"

sl "github.com/mrf345/safelock-cli/safelock"
)
Expand All @@ -11,6 +12,13 @@ func (a *App) updateStatus(status string, percent float64) {
a.task.percent = percent

if percent > 0.0 {
WindowSetTitle(
a.ctx, fmt.Sprintf(
"%sing (%.2f%%)",
strings.Title(a.task.kind.Str()), //nolint:all
percent,
),
)
EventsEmit(
a.ctx,
statusUpdateKey,
Expand All @@ -23,10 +31,11 @@ func (a *App) updateStatus(status string, percent float64) {
func (a *App) resetTask() {
a.offTaskHandlers()
EventsEmit(a.ctx, statusEndKey)
WindowSetTitle(a.ctx, Name)
a.task = Task{}
}

func (a *App) offTaskHandlers() {
func (a App) offTaskHandlers() {
if a.task.lock != nil {
a.task.lock.StatusObs.
Off(sl.StatusUpdate.Str(), a.updateStatus).
Expand Down
1 change: 0 additions & 1 deletion frontend/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ declare global {
OnFileDrop(callback: (x: number, y: number, paths: string[]) => void, useDropTarget: boolean) :void
OnFileDropOff(): void
BrowserOpenURL(url: string): void
WindowSetTitle(title: string): void
}
}
}
13 changes: 12 additions & 1 deletion frontend/setup-jest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
declare global {
interface Window {
runtime: {
EventsOn(eventName: string, callback: (...optionalData: any[]) => void): void,
EventsOff(eventName: string, ...additionalEvents: string[]): void
OnFileDrop(callback: (x: number, y: number, paths: string[]) => void, useDropTarget: boolean) :void
OnFileDropOff(): void
BrowserOpenURL(url: string): void
}
}
}

import 'jest-preset-angular/setup-jest';
import './global';
import './jest-global-mocks';
4 changes: 0 additions & 4 deletions frontend/src/app/services/link.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ export class LinkService {
window?.runtime?.BrowserOpenURL(url);
}

setTitle(title: string) {
window?.runtime?.WindowSetTitle(title);
}

removeDroppedListeners() {
window?.runtime?.OnFileDropOff();
}
Expand Down
10 changes: 0 additions & 10 deletions frontend/src/app/services/task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@ export class TaskService {
}

cancel(): Observable<void> {
this.restoreDefaultTitle();
return from(Cancel()).pipe(tap(() => this.remove()));
}

remove() {
this.stopStatusUpdate();
this._task = new Task();
this.task$.next(this._task);
this.restoreDefaultTitle();
}

setPassword(pwd: string) {
Expand All @@ -97,10 +95,6 @@ export class TaskService {
window?.runtime?.EventsOn(AppEvents.statusUpdateKey, (status: string, percent: string) => {
this._task?.status$.next(status);
this._task?.percent$.next(percent);
this.linkService.setTitle(
`${this.titleCase(this._task?.kind$.getValue())}ing` +
` (${percent}%)`
);
});
window?.runtime?.EventsOn(AppEvents.statusEndKey, () => {
if (this._task.isCreated) {
Expand All @@ -109,8 +103,4 @@ export class TaskService {
}
});
}

private restoreDefaultTitle() {
this.linkService.setTitle('Safelock');
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22

require (
github.com/google/uuid v1.3.0
github.com/mrf345/desktop-entry v0.1.0
github.com/mrf345/desktop-entry v0.2.0
github.com/mrf345/safelock-cli v0.4.4
github.com/stretchr/testify v1.9.0
github.com/wailsapp/wails/v2 v2.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APP
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mholt/archiver/v4 v4.0.0-alpha.8 h1:tRGQuDVPh66WCOelqe6LIGh0gwmfwxUrSSDunscGsRM=
github.com/mholt/archiver/v4 v4.0.0-alpha.8/go.mod h1:5f7FUYGXdJWUjESffJaYR4R60VhnHxb2X3T1teMyv5A=
github.com/mrf345/desktop-entry v0.1.0 h1:3O0iGQEPZNY8w9NaLJBkAlwMcUzWVS+DSKj1/s/hqQU=
github.com/mrf345/desktop-entry v0.1.0/go.mod h1:I05CBP5fSBv7fa3ImKWBY5+0DEpbPzzzih+5y1XaYag=
github.com/mrf345/desktop-entry v0.2.0 h1:6RV6Rk1jpSngMtSvKgNL61mWbIRyAoczFheVn7diodE=
github.com/mrf345/desktop-entry v0.2.0/go.mod h1:I05CBP5fSBv7fa3ImKWBY5+0DEpbPzzzih+5y1XaYag=
github.com/mrf345/safelock-cli v0.4.4 h1:VpNNR2i2TzjfdKKAi/R0a0yZIL9UgxCizBMqhLABE/I=
github.com/mrf345/safelock-cli v0.4.4/go.mod h1:5JSG/xryFFU2+lxaca6j5JlPq1YcBrhuJmxhEJtkMHM=
github.com/mrf345/wails/v2 v2.0.0-20240906143800-035eb7c1ed2c h1:WNoJ0oF8O6uf+VsVf7AWBST9SMWHuBOb2+OC1m+7GaU=
Expand Down
11 changes: 11 additions & 0 deletions wails.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,16 @@
"author": {
"name": "Mohamed Feddad",
"email": "mrf345@gmail.com"
},
"info": {
"fileAssociations": [
{
"ext": "sla",
"name": "Safelock",
"description": "Safelock Encrypted File",
"iconName": "appicon",
"role": "Viewer"
}
]
}
}

0 comments on commit 877c338

Please sign in to comment.