Skip to content

Commit

Permalink
fix(server): improved file dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
sr258 committed Apr 5, 2021
1 parent cb33fcb commit 5b13df9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
16 changes: 11 additions & 5 deletions server/src/controllers/LumiController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dialog } from 'electron';
import { BrowserWindow, dialog } from 'electron';
import fs from 'fs-extra';
import _path from 'path';

Expand All @@ -15,7 +15,11 @@ import electronState from '../electronState';
const log = new Logger('controller:lumi-h5p');

export default class LumiController {
constructor(private h5pEditor: H5P.H5PEditor, serverConfig: IServerConfig) {
constructor(
private h5pEditor: H5P.H5PEditor,
serverConfig: IServerConfig,
private browserWindow: BrowserWindow
) {
fs.readJSON(serverConfig.settingsFile).then((settings) => {
if (settings.privacyPolicyConsent) {
h5pEditor.contentTypeCache.updateIfNecessary();
Expand All @@ -35,16 +39,18 @@ export default class LumiController {
let path = pathArg;

if (!path || path === 'undefined') {
path = dialog.showSaveDialogSync({
const result = await dialog.showSaveDialog(this.browserWindow, {
defaultPath: '.h5p',
filters: [
{
extensions: ['h5p'],
name: 'H5P'
}
],
title: 'Save H5P'
title: 'Save H5P',
properties: ['showOverwriteConfirmation']
});
path = result.filePath;
}

if (!path) {
Expand Down Expand Up @@ -129,7 +135,7 @@ export default class LumiController {
}

public async open(): Promise<string[]> {
const response = await dialog.showOpenDialog({
const response = await dialog.showOpenDialog(this.browserWindow, {
filters: [
{
extensions: ['h5p'],
Expand Down
6 changes: 3 additions & 3 deletions server/src/routes/analyticRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import { dialog } from 'electron';
import { BrowserWindow, dialog } from 'electron';
import * as Sentry from '@sentry/electron';
import fs from 'fs';
import recursiveReaddir from 'recursive-readdir';
Expand All @@ -14,12 +14,12 @@ import {

import _path from 'path';

export default function (): express.Router {
export default function (browserWindow: BrowserWindow): express.Router {
const router = express.Router();

router.get('/', async (req: express.Request, res) => {
try {
const openDialog = await dialog.showOpenDialog({
const openDialog = await dialog.showOpenDialog(browserWindow, {
properties: ['openDirectory']
});

Expand Down
12 changes: 9 additions & 3 deletions server/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function (
log.info('setting up routes');

router.use('/api/v1/track', trackingRoutes(serverConfig));
router.use('/api/v1/analytics', analyticRoutes());
router.use('/api/v1/analytics', analyticRoutes(browserWindow));

// Adding dummy user to make sure all requests can be handled
router.use((req, res, next) => {
Expand All @@ -46,7 +46,10 @@ export default function (
settingsRoutes(serverConfig, browserWindow, app)
);

router.use('/api/v1/run', runRoutes(serverConfig, h5pEditor));
router.use(
'/api/v1/run',
runRoutes(serverConfig, h5pEditor, browserWindow)
);

// // Directly serving the library and content files statically speeds up
// // loading times and there is no security issue, as Lumi never is a
Expand Down Expand Up @@ -128,7 +131,10 @@ export default function (
contentTypeCacheExpressRouter(h5pEditor.contentTypeCache)
);

router.use('/api/v1/lumi', lumiRoutes(h5pEditor, serverConfig));
router.use(
'/api/v1/lumi',
lumiRoutes(h5pEditor, serverConfig, browserWindow)
);

router.get('*', express.static(`${__dirname}/../../client`));

Expand Down
10 changes: 8 additions & 2 deletions server/src/routes/lumiRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import * as Sentry from '@sentry/node';
import LumiController from '../controllers/LumiController';
import Logger from '../helpers/Logger';
import IServerConfig from '../IServerConfig';
import { BrowserWindow } from 'electron';

const log = new Logger('routes:lumi-h5p');

export default function (
h5pEditor: H5PEditor,
serverConfig: IServerConfig
serverConfig: IServerConfig,
browserWindow: BrowserWindow
): express.Router {
const router = express.Router();
const lumiController = new LumiController(h5pEditor, serverConfig);
const lumiController = new LumiController(
h5pEditor,
serverConfig,
browserWindow
);

router.get(
'/package/:contentId',
Expand Down
26 changes: 15 additions & 11 deletions server/src/routes/runRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express';
import * as Sentry from '@sentry/node';
import IServerConfig from '../IServerConfig';
import { dialog } from 'electron';
import { BrowserWindow, dialog } from 'electron';
import User from '../User';
import fs from 'fs-extra';
import path from 'path';
Expand All @@ -14,7 +14,8 @@ import { io as websocket } from '../websocket';

export default function (
serverConfig: IServerConfig,
h5pEditor: H5P.H5PEditor
h5pEditor: H5P.H5PEditor,
browserWindow: BrowserWindow
): express.Router {
const router = express.Router();
router.get(
Expand Down Expand Up @@ -67,15 +68,18 @@ export default function (
let filePath: string =
req.query.filePath && `${req.query.filePath}`;
if (!filePath) {
const { filePaths } = await dialog.showOpenDialog({
filters: [
{
extensions: ['h5p'],
name: 'HTML 5 Package'
}
],
properties: ['openFile']
});
const { filePaths } = await dialog.showOpenDialog(
browserWindow,
{
filters: [
{
extensions: ['h5p'],
name: 'HTML 5 Package'
}
],
properties: ['openFile']
}
);

filePath = filePaths[0];
}
Expand Down

0 comments on commit 5b13df9

Please sign in to comment.