Skip to content

Commit

Permalink
Add new shell API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Jul 28, 2023
1 parent 7a944d8 commit b03302b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dist/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const window_1 = __importDefault(require("./api/window"));
const process_1 = __importDefault(require("./api/process"));
const contextMenu_1 = __importDefault(require("./api/contextMenu"));
const settings_1 = __importDefault(require("./api/settings"));
const shell_1 = __importDefault(require("./api/shell"));
const progressBar_1 = __importDefault(require("./api/progressBar"));
function startAPIServer(randomSecret) {
return __awaiter(this, void 0, void 0, function* () {
Expand All @@ -53,6 +54,7 @@ function startAPIServer(randomSecret) {
httpServer.use("/api/window", window_1.default);
httpServer.use("/api/process", process_1.default);
httpServer.use("/api/settings", settings_1.default);
httpServer.use("/api/shell", shell_1.default);
httpServer.use("/api/context", contextMenu_1.default);
httpServer.use("/api/menu-bar", menuBar_1.default);
httpServer.use("/api/progress-bar", progressBar_1.default);
Expand Down
52 changes: 52 additions & 0 deletions dist/server/api/shell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const electron_1 = require("electron");
const router = express_1.default.Router();
router.post("/show-item-in-folder", (req, res) => {
const { path } = req.body;
electron_1.shell.showItemInFolder(path);
res.sendStatus(200);
});
router.post("/open-item", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { path } = req.body;
let result = yield electron_1.shell.openPath(path);
res.json({
result
});
}));
router.post("/open-external", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { url } = req.body;
try {
yield electron_1.shell.openExternal(url);
res.sendStatus(200);
}
catch (e) {
res.status(500).json({
error: e
});
}
}));
router.delete("/trash-item", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { path } = req.body;
try {
yield electron_1.shell.trashItem(path);
res.sendStatus(200);
}
catch (e) {
res.status(400).json();
}
}));
exports.default = router;
2 changes: 2 additions & 0 deletions src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import windowRoutes from "./api/window";
import processRoutes from "./api/process";
import contextMenuRoutes from "./api/contextMenu";
import settingsRoutes from "./api/settings";
import shellRoutes from "./api/shell";
import progressBarRoutes from "./api/progressBar";
import { Server } from "net";

Expand Down Expand Up @@ -47,6 +48,7 @@ async function startAPIServer(randomSecret: string): Promise<APIProcess> {
httpServer.use("/api/window", windowRoutes);
httpServer.use("/api/process", processRoutes);
httpServer.use("/api/settings", settingsRoutes);
httpServer.use("/api/shell", shellRoutes);
httpServer.use("/api/context", contextMenuRoutes);
httpServer.use("/api/menu-bar", menuBarRoutes);
httpServer.use("/api/progress-bar", progressBarRoutes);
Expand Down
50 changes: 50 additions & 0 deletions src/server/api/shell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import express from "express";
import { shell } from "electron";

const router = express.Router();

router.post("/show-item-in-folder", (req, res) => {
const { path } = req.body;

shell.showItemInFolder(path);

res.sendStatus(200);
});

router.post("/open-item", async (req, res) => {
const { path } = req.body;

let result = await shell.openPath(path);

res.json({
result
})
});

router.post("/open-external", async (req, res) => {
const { url } = req.body;

try {
await shell.openExternal(url);

res.sendStatus(200);
} catch (e) {
res.status(500).json({
error: e
});
}
});

router.delete("/trash-item", async (req, res) => {
const { path } = req.body;

try {
await shell.trashItem(path);

res.sendStatus(200);
} catch (e) {
res.status(400).json();
}
});

export default router;

0 comments on commit b03302b

Please sign in to comment.