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

413 tiga basic pwd 测试1 #418

Merged
merged 10 commits into from
Sep 19, 2023
Merged
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# icons
icons/*
!icons/create_icon.sh
!icons/1024x1024.png

.idea

# prisma
Expand Down
16 changes: 16 additions & 0 deletions apps/electron/electron.vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { resolve } from "path";
import { defineConfig, externalizeDepsPlugin } from "electron-vite";
import { sentryVitePlugin } from "@sentry/vite-plugin";
import react from "@vitejs/plugin-react";

const IS_DEV = process.env.NODE_ENV === "development";

export default defineConfig({
main: {
build: {
sourcemap: true,
},
plugins: [
externalizeDepsPlugin({
exclude: [
Expand All @@ -20,6 +26,16 @@ export default defineConfig({
"sharp",
],
}),

!IS_DEV &&
sentryVitePlugin({
org: "meetqy",
project: "rao-pics",
// Auth tokens can be obtained from https://sentry.io/settings/account/api/auth-tokens/
// and need `project:releases` and `org:read` scopes
authToken:
"7979f62cdaee7ba46204863b5777bec04eed627ccb7a2c5d32262ad3a04672e7",
}),
],
},
preload: {
Expand Down
5 changes: 4 additions & 1 deletion apps/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
"build:mac": "pnpm clean:dist && pnpm build && electron-builder --mac --config ./electron-builder.cjs",
"build:win": "pnpm clean:dist && pnpm build && electron-builder --win --config",
"clean": "git clean -xdf .turbo node_modules out dist",
"clean:dist": "git clean -xdf dist",
"clean:dist": "git clean -xdf dist/*",
"dev": "electron-vite dev",
"format": "prettier --check \"**/*.{mjs,ts,md,json}\"",
"postinstall": "electron-builder install-app-deps",
"lint": "eslint .",
"start": "electron-vite preview",
"typecheck": "pnpm typecheck:node && pnpm typecheck:web",
Expand Down Expand Up @@ -50,6 +51,8 @@
"@rao-pics/prettier-config": "workspace:^",
"@rao-pics/tailwind-config": "workspace:*",
"@rao-pics/tsconfig": "^0.1.0",
"@sentry/electron": "^4.11.0",
"@sentry/vite-plugin": "^2.7.1",
"@types/ip": "^1.1.0",
"@types/lodash": "^4.14.197",
"@types/node": "^18.17.6",
Expand Down
77 changes: 41 additions & 36 deletions apps/electron/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { join } from "path";
import { app, BrowserWindow, dialog, shell } from "electron";
import { createIPCHandler } from "electron-trpc/main";
import { electronApp, optimizer } from "@electron-toolkit/utils";
import * as Sentry from "@sentry/electron";
import getPort, { portNumbers } from "get-port";
import ip from "ip";

Expand All @@ -14,13 +15,20 @@ import { createDbPath } from "@rao-pics/db";
import icon from "../../resources/icon.png?asset";
import { createCustomIPCHandle } from "./src/ipc";

const caller = router.createCaller({});
/**
* Sentry init
*/
Sentry.init({
dsn: "https://178a415c4ef2421a8f52b6c4041319af@o4505321607397376.ingest.sentry.io/4505321612705792",
debug: IS_DEV,
});

const controller = new AbortController();
const { signal } = controller;

// 获取端口
async function initConfig() {
const caller = router.createCaller({});
const config = await caller.config.get();

const serverPort =
Expand All @@ -31,9 +39,39 @@ async function initConfig() {
return await caller.config.upsert({
serverPort,
clientPort,
ip: ip.address(),
});
}

const mainWindowReadyToShow = async () => {
const config = await initConfig();
await startExpressServer();

const { clientPort } = config;

if (!clientPort) return;

if (!IS_DEV) {
const child = cp.fork(
join(
process.resourcesPath,
"themes",
config?.theme ?? DEFAULT_THEME,
"server.js",
),
["child"],
{
env: { PORT: clientPort.toString(), HOSTNAME: "0.0.0.0" },
signal,
},
);

child.on("error", (e) => {
console.error(e);
});
}
};

function createWindow(): void {
// Create the browser window.
const mainWindow = new BrowserWindow({
Expand All @@ -58,9 +96,6 @@ function createWindow(): void {

mainWindow.on("ready-to-show", () => {
mainWindow.show();
void caller.config.upsert({
ip: ip.address(),
});
});

mainWindow.webContents.setWindowOpenHandler((details) => {
Expand All @@ -78,8 +113,8 @@ function createWindow(): void {
void mainWindow.loadFile(join(__dirname, "../renderer/index.html"));
}

void mainWindowReadyToShow();
createIPCHandler({ router, windows: [mainWindow] });

createCustomIPCHandle();
}

Expand All @@ -88,40 +123,10 @@ function createWindow(): void {
// Some APIs can only be used after this event occurs.
app
.whenReady()
.then(async () => {
.then(() => {
// Set app user model id for windows
electronApp.setAppUserModelId("com.rao-pics");

const config = await initConfig();
await startExpressServer();
// 启动静态资源服务器
// await startStaticServer();

const { clientPort } = config;

if (!clientPort) return;

if (!IS_DEV) {
const child = cp.fork(
join(
process.resourcesPath,
"themes",
config?.theme ?? DEFAULT_THEME,
"server.js",
),
["child"],
{
env: { PORT: clientPort.toString(), HOSTNAME: "0.0.0.0" },
signal,
},
);

child.on("error", (e) => {
dialog.showErrorBox("child process fork error", e.message);
controller.abort();
});
}

// Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production.
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
Expand Down
Binary file added icons/1024x1024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
132 changes: 132 additions & 0 deletions icons/create_icon.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/bin/bash

set -eo pipefail

# Config
SOURCE_FILE_PATH='./1024x1024.png' # has to be of size 1024x1024 px
OUT_ICON_NAME='icon'


# The "design" and magic numbers below are derived from Apple's macOS app icon
# guidelines and design templates:
# https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/app-icon/
# https://developer.apple.com/design/resources/#macos-apps
#
# Specifically, for an icon of 1024x:
# - outer bounding box: 1024x1024 px
# - border radius: ~22,85% (= 234 px)
# - icon grid size: 824x824 px
# - icon grid shadow size: x: 0px, y: 10px, blur: 10px, 30% black


# Make sure ImageMagick's convert and iconutil are available.
if ! hash convert 2>/dev/null || ! hash iconutil 2>/dev/null; then
echo "ERROR: This script requires ImageMagick and iconutil."
exit 1
fi


# Prepare an iconset folder
mkdir "./${OUT_ICON_NAME}.iconset"
mkdir "./${OUT_ICON_NAME}_shadow_rounded.iconset"
mkdir "./${OUT_ICON_NAME}_rounded.iconset"


# Add rounded corners to the 1024px image.
#
# This works by:
# 1. Generating a black square (1024 px) with rounded corners (radius 234 px)
# on transparent background, via `-size [...] xc:none -draw [...]`
# 2. Applying the square as a mask to the the source image, via `-matte [...]`
convert "${SOURCE_FILE_PATH}" \
-matte \( \
-size 1024x1024 xc:none -draw "roundrectangle 0,0,1024,1024,234,234" \
\) \
-compose DstIn -composite \
"./${OUT_ICON_NAME}.iconset/temp_1024_rounded.png"


convert "${SOURCE_FILE_PATH}" \
-resize 824x824 \
"./${OUT_ICON_NAME}.iconset/temp_1024.png"


# Apply sizing and add shadow to the 1024px image.
#
# This works by:
# 1. Resizing to 'icon grid size' (824px), via `-resize`
# 2. Adding padding (100px) to get 'outer bounding box' size,
# via `-bordercolor none -border [...]`
# 3. Adding shadow, via `+clone -background black -shadow [...]`
convert "./${OUT_ICON_NAME}.iconset/temp_1024_rounded.png" \
-resize 824x824 \
-bordercolor none -border 100x100 \
\( +clone -background black -shadow 30x10+0+10 -background none \) \
-compose DstOver -flatten \
"./${OUT_ICON_NAME}_shadow_rounded.iconset/[email protected]"

convert "./${OUT_ICON_NAME}.iconset/temp_1024_rounded.png" \
-resize 824x824 \
-bordercolor none -border 0x0 \
\( +clone -background none \) \
-compose DstOver -flatten \
"./${OUT_ICON_NAME}_rounded.iconset/[email protected]"

convert "./${OUT_ICON_NAME}.iconset/temp_1024.png" \
-resize 824x824 \
"./${OUT_ICON_NAME}.iconset/[email protected]"

# Remove temporary file
rm "./${OUT_ICON_NAME}.iconset/temp_1024_rounded.png"
rm "./${OUT_ICON_NAME}.iconset/temp_1024.png"

# Generate all sizes.
# 16/32/128/256/512, each single & double resolution
cd "./${OUT_ICON_NAME}.iconset/"
convert './[email protected]' \
\( +clone -resize x16 -write './icon_16x16.png' +delete \) \
\( +clone -resize x32 -write './[email protected]' +delete \) \
\( +clone -resize x32 -write './icon_32x32.png' +delete \) \
\( +clone -resize x64 -write './[email protected]' +delete \) \
\( +clone -resize x128 -write './icon_128x128.png' +delete \) \
\( +clone -resize x256 -write './[email protected]' +delete \) \
\( +clone -resize x256 -write './icon_256x256.png' +delete \) \
\( +clone -resize x512 -write './[email protected]' +delete \) \
-resize x512 './icon_512x512.png'
cd '..'

cd "./${OUT_ICON_NAME}_shadow_rounded.iconset/"
convert './[email protected]' \
\( +clone -resize x16 -write './icon_16x16.png' +delete \) \
\( +clone -resize x32 -write './[email protected]' +delete \) \
\( +clone -resize x32 -write './icon_32x32.png' +delete \) \
\( +clone -resize x64 -write './[email protected]' +delete \) \
\( +clone -resize x128 -write './icon_128x128.png' +delete \) \
\( +clone -resize x256 -write './[email protected]' +delete \) \
\( +clone -resize x256 -write './icon_256x256.png' +delete \) \
\( +clone -resize x512 -write './[email protected]' +delete \) \
-resize x512 './icon_512x512.png'
cd '..'

cd "./${OUT_ICON_NAME}_rounded.iconset/"
convert './[email protected]' \
\( +clone -resize x16 -write './icon_16x16.png' +delete \) \
\( +clone -resize x32 -write './[email protected]' +delete \) \
\( +clone -resize x32 -write './icon_32x32.png' +delete \) \
\( +clone -resize x64 -write './[email protected]' +delete \) \
\( +clone -resize x128 -write './icon_128x128.png' +delete \) \
\( +clone -resize x256 -write './[email protected]' +delete \) \
\( +clone -resize x256 -write './icon_256x256.png' +delete \) \
\( +clone -resize x512 -write './[email protected]' +delete \) \
-resize x512 './icon_512x512.png'
cd '..'

# Convert to .icns format and remove iconset
iconutil -c icns "./${OUT_ICON_NAME}.iconset"
# rm -r "./${OUT_ICON_NAME}.iconset"

mkdir icons
mv "./${OUT_ICON_NAME}.icns" "./icons/${OUT_ICON_NAME}.icns"
mv "./${OUT_ICON_NAME}_shadow_rounded.iconset" "./icons/${OUT_ICON_NAME}_shadow_rounded"
mv "./${OUT_ICON_NAME}_rounded.iconset" "./icons/${OUT_ICON_NAME}_rounded"
mv "./${OUT_ICON_NAME}.iconset" "./icons/${OUT_ICON_NAME}"
2 changes: 2 additions & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"express": "^4.18.2",
"fs-extra": "^11.1.1",
"lodash": "^4.17.21",
"plaiceholder": "^3.0.0",
"sharp": "^0.32.5",
"superjson": "1.13.1",
"zod": "^3.22.2"
},
Expand Down
23 changes: 13 additions & 10 deletions packages/api/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import express from "express";
import { readFileSync } from "fs-extra";
import { getPlaiceholder } from "plaiceholder";

import type { Library } from "@rao-pics/db";

import { router } from "..";
import { createContext } from "./utils";

Expand All @@ -22,6 +24,7 @@ const asyncMiddleware =

let server: Server<typeof IncomingMessage, typeof ServerResponse> | undefined;
let libraryPath: string | undefined;
let library: Library | null;

export const startExpressServer = async () => {
if (server) return;
Expand All @@ -30,7 +33,7 @@ export const startExpressServer = async () => {

const caller = router.createCaller({});
const config = await caller.config.get();
const library = await caller.library.get();
library = await caller.library.get();

const port = config?.serverPort;

Expand Down Expand Up @@ -63,15 +66,15 @@ export const startExpressServer = async () => {
*/
app.use("/static/", (req, res, next) => {
if (!library) {
return res.status(404).send("library is not defined");
res.status(404).send("library is not defined");
} else {
libraryPath = join(library.path, "images");
express.static(libraryPath, { maxAge: 180 * 1000 * 60 * 60 })(
req,
res,
next,
);
}

libraryPath = join(library.path, "images");
express.static(libraryPath, { maxAge: 180 * 1000 * 60 * 60 })(
req,
res,
next,
);
});

/**
Expand Down Expand Up @@ -130,7 +133,7 @@ export const startExpressServer = async () => {

export const updateLibraryPath = async () => {
const caller = router.createCaller({});
const library = await caller.library.get();
library = await caller.library.get();

if (library?.path) {
libraryPath = join(library.path, "images");
Expand Down
Loading