Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/i144 image posting #156

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/bluesky-social/indigo/xrpc"
"github.com/gen2brain/beeep"
"github.com/go-co-op/gocron/v2"
"github.com/h2non/bimg"

"github.com/wailsapp/wails/v2/pkg/runtime"
)

Expand Down Expand Up @@ -129,14 +131,41 @@ func NewHttpClient() *http.Client {
}
}

func (a *App) UploadImage(path string) ([]byte, error) {
// Upload image to the server.

f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()

img, err := io.ReadAll(f)
if err != nil {
return nil, err
}
img, err = bimg.NewImage(img).Process(bimg.Options{StripMetadata: true})
if err != nil {
return nil, err
}
res, err := BskyUploadBlob(a.xrpcc, img)
if err != nil {
return nil, err
}
a.logger.Info("Uploaded image", "result", res, "ref", res.Blob.Ref, "mimetype", res.Blob.MimeType, "size", res.Blob.Size)

// BskyFeedPost(a.xrpcc, "Uploaded image", []util.LexBlob{{Ref: res.Blob.Ref, MimeType: res.Blob.MimeType, Size: res.Blob.Size}})
return res.Blob.MarshalJSON()
}

func (a *App) Post(text string) string {
a.logger.Info("Post", "text", text)
beeep.Notify("まぜそば大陸", fmt.Sprintf("BskyFeedPost: %s", text), "")
beeep.Notify("まぜそば大陸", fmt.Sprintf("Post: %s", text), "")

if a.environment.BuildType == "dev" {
return "<MOCK URI>"
}
res, err := BskyFeedPost(a.xrpcc, text)
res, err := BskyFeedPost(a.xrpcc, text, nil)
if err != nil {
errs := fmt.Sprintf("Posting error: %s", err.Error())
a.logger.Error("Post: Error on BskyFeedPost", "error", errs)
Expand Down
24 changes: 23 additions & 1 deletion bluesky.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ func addLink(xrpcc *xrpc.Client, post *bsky.FeedPost, link string) {
}
}

func BskyFeedPost(xrpcc *xrpc.Client, text string) (string, error) {
func BskyUploadBlob(xrpcc *xrpc.Client, b []byte) (*comatproto.RepoUploadBlob_Output, error) {
resp, err := comatproto.RepoUploadBlob(context.TODO(), xrpcc, bytes.NewReader(b))
if err != nil {
return nil, fmt.Errorf("failed to upload blob: %w", err)
}
return resp, nil
}

func BskyFeedPost(xrpcc *xrpc.Client, text string, images []lexutil.LexBlob) (string, error) {
// Post given text to Bluesky, with app.bsky.feed.post.
if text == "" || strings.TrimSpace(text) == "" {
return "", fmt.Errorf("no text specified")
Expand Down Expand Up @@ -141,6 +149,20 @@ func BskyFeedPost(xrpcc *xrpc.Client, text string) (string, error) {
})
}

if len(images) > 0 {
var imageRefs []*bsky.EmbedImages_Image
for _, image := range images {
imageRefs = append(imageRefs, &bsky.EmbedImages_Image{
Image: &image,
})
}
if post.Embed == nil {
post.Embed = &bsky.FeedPost_Embed{}
}
post.Embed.EmbedImages = &bsky.EmbedImages{
Images: imageRefs,
}
}
resp, err := comatproto.RepoCreateRecord(context.TODO(), xrpcc, &comatproto.RepoCreateRecord_Input{
Collection: "app.bsky.feed.post",
Repo: xrpcc.Auth.Did,
Expand Down
2 changes: 1 addition & 1 deletion frontend/package.json.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9623b8af402d17551ec68e258c609047
ba45844b7d25f3d7453dba2a5d2db6cc
55 changes: 44 additions & 11 deletions frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@
OpenLogDirectory,
GetContext,
Chikuwa,
UploadImage,
Post,
} from "../wailsjs/go/main/App";
import { Environment } from "../wailsjs/runtime";
import InputBox from "./components/InputBox.svelte";
import StatusBar from "./components/StatusBar.svelte";

// import {
// EventsOn,
// EventsEmit,
// WindowCenter,
// WindowHide,
// WindowShow,
// Quit,
// LogInfo,
// LogWarning,
// } from "../wailsjs/runtime/runtime";
import {
EventsOn,
EventsEmit,
WindowCenter,
WindowHide,
WindowShow,
Quit,
LogInfo,
LogWarning,
OnFileDrop,
} from "../wailsjs/runtime/runtime";
import { dispatchInput } from "./dispatchInput.js";

import { ConvertRichUnicode } from "./topping/unicode";

struct : {}
let input: string = "";
let images
let helpMessage = "Ready";
let charCount = 0;
let charCount = 0; // -1 to hide CharCounter
let placeholder = Math.random() > 0.5 ? "最近どう?" : "どう最近?";


Expand All @@ -39,6 +44,33 @@
return Promise.reject(err);
});

OnFileDrop((_x, _y, paths) => {
LogInfo(`Dropped: ${paths}, ${_x}, ${_y}`);
placeholder = '[' + paths.join(", ") + ']';
input = paths.join(", ");
let foo = '';
UploadImage(paths[0])
.then((result) => {
// result is base64 encoded, so decode it.
LogInfo(result)
foo = result;
result = atob(result);
LogInfo(result)
input = result;
})
.catch((error) => {
helpMessage = `Error: ${error}`;
});
PostWithImage(input, foo)
.then((result) => {
placeholder = result;
})
.catch((error) => {
helpMessage = `Error: ${error}`;
});
LogInfo('ende')
}, true);

function clearText() {
input = "";
charCount = -1;
Expand Down Expand Up @@ -130,6 +162,7 @@
<style>
body {
--wails-draggable: drag;
--wails-drop-target: drop;
background-color: rgba(27, 38, 54, 0.5);
height: 100vh;
width: 100vw;
Expand Down
2 changes: 2 additions & 0 deletions frontend/wailsjs/go/main/App.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export function PostAndForget(arg1:string):Promise<void>;
export function SetScheduler():Promise<void>;

export function SetXRPCClient():Promise<void>;

export function UploadImage(arg1:string):Promise<Array<number>>;
4 changes: 4 additions & 0 deletions frontend/wailsjs/go/main/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ export function SetScheduler() {
export function SetXRPCClient() {
return window['go']['main']['App']['SetXRPCClient']();
}

export function UploadImage(arg1) {
return window['go']['main']['App']['UploadImage'](arg1);
}
14 changes: 14 additions & 0 deletions frontend/wailsjs/runtime/runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,17 @@ export function ClipboardGetText(): Promise<string>;
// [ClipboardSetText](https://wails.io/docs/reference/runtime/clipboard#clipboardsettext)
// Sets a text on the clipboard
export function ClipboardSetText(text: string): Promise<boolean>;

// [OnFileDrop](https://wails.io/docs/reference/runtime/draganddrop#onfiledrop)
// OnFileDrop listens to drag and drop events and calls the callback with the coordinates of the drop and an array of path strings.
export function OnFileDrop(callback: (x: number, y: number ,paths: string[]) => void, useDropTarget: boolean) :void

// [OnFileDropOff](https://wails.io/docs/reference/runtime/draganddrop#dragandddropoff)
// OnFileDropOff removes the drag and drop listeners and handlers.
export function OnFileDropOff() :void

// Check if the file path resolver is available
export function CanResolveFilePaths(): boolean;

// Resolves file paths for an array of files
export function ResolveFilePaths(files: File[]): void
36 changes: 36 additions & 0 deletions frontend/wailsjs/runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,40 @@ export function ClipboardGetText() {

export function ClipboardSetText(text) {
return window.runtime.ClipboardSetText(text);
}

/**
* Callback for OnFileDrop returns a slice of file path strings when a drop is finished.
*
* @export
* @callback OnFileDropCallback
* @param {number} x - x coordinate of the drop
* @param {number} y - y coordinate of the drop
* @param {string[]} paths - A list of file paths.
*/

/**
* OnFileDrop listens to drag and drop events and calls the callback with the coordinates of the drop and an array of path strings.
*
* @export
* @param {OnFileDropCallback} callback - Callback for OnFileDrop returns a slice of file path strings when a drop is finished.
* @param {boolean} [useDropTarget=true] - Only call the callback when the drop finished on an element that has the drop target style. (--wails-drop-target)
*/
export function OnFileDrop(callback, useDropTarget) {
return window.runtime.OnFileDrop(callback, useDropTarget);
}

/**
* OnFileDropOff removes the drag and drop listeners and handlers.
*/
export function OnFileDropOff() {
return window.runtime.OnFileDropOff();
}

export function CanResolveFilePaths() {
return window.runtime.CanResolveFilePaths();
}

export function ResolveFilePaths(files) {
return window.runtime.ResolveFilePaths(files);
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/bluesky-social/indigo v0.0.0-20240301203549-d06d65d92df6
github.com/gen2brain/beeep v0.0.0-20240112042604-c7bb2cd88fea
github.com/go-co-op/gocron/v2 v2.11.0
github.com/h2non/bimg v1.1.9
github.com/wailsapp/wails/v2 v2.9.1
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/h2non/bimg v1.1.9 h1:WH20Nxko9l/HFm4kZCA3Phbgu2cbHvYzxwxn9YROEGg=
github.com/h2non/bimg v1.1.9/go.mod h1:R3+UiYwkK4rQl6KVFTOFJHitgLbZXBZNFh2cv3AEbp8=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func main() {
Bind: []interface{}{
app,
},
DragAndDrop: &options.DragAndDrop{
EnableFileDrop: true,
DisableWebViewDrop: false,
},
Windows: &windows.Options{
WebviewIsTransparent: true,
WindowIsTranslucent: true,
Expand Down
Loading