From 8f2bc2ec5069ff3c22d8d3aa26726024ca377fe4 Mon Sep 17 00:00:00 2001 From: liweitian Date: Wed, 20 Nov 2019 02:06:57 +0800 Subject: [PATCH] feat: update default bots folder (#1548) * save tmp code * save tmp code * save current path in data.json * initialize default path according to OS * use default path if current path does not work * fix bug about updating last accessed path * create default folder * handle comments * handle comments * fix lint error * fix test case * fix test case --- .../LocationBrowser/LocationSelectContent.tsx | 10 ++----- .../client/src/store/action/storage.ts | 4 +++ .../server/__tests__/services/project.test.ts | 1 + .../server/__tests__/services/storage.test.ts | 1 + .../server/src/controllers/storage.ts | 6 ++++ Composer/packages/server/src/router/api.ts | 1 + .../packages/server/src/services/storage.ts | 30 +++++++++++++++++-- .../server/src/store/data.template.ts | 4 ++- 8 files changed, 47 insertions(+), 10 deletions(-) diff --git a/Composer/packages/client/src/CreationFlow/LocationBrowser/LocationSelectContent.tsx b/Composer/packages/client/src/CreationFlow/LocationBrowser/LocationSelectContent.tsx index a172cc6a5f..b8dad48093 100644 --- a/Composer/packages/client/src/CreationFlow/LocationBrowser/LocationSelectContent.tsx +++ b/Composer/packages/client/src/CreationFlow/LocationBrowser/LocationSelectContent.tsx @@ -7,14 +7,10 @@ import path from 'path'; import { jsx } from '@emotion/core'; import { Fragment, useEffect, useState, useContext, useRef } from 'react'; -import storage from '../../utils/storage'; - import { FileSelector } from './FileSelector'; import { StoreContext } from './../../store'; import { FileTypes } from './../../constants'; -const NEW_BOT_LOCATION_KEY = 'newBotLocation'; - export function LocationSelectContent(props) { const { state, actions } = useContext(StoreContext); const { storages, focusedStorageFolder, storageFileLoadingStatus } = state; @@ -22,7 +18,7 @@ export function LocationSelectContent(props) { const { fetchFolderItemsByPath } = actions; const currentStorageIndex = useRef(0); - const [currentPath, setCurrentPath] = useState(storage.get(NEW_BOT_LOCATION_KEY, '')); + const [currentPath, setCurrentPath] = useState(''); const currentStorageId = storages[currentStorageIndex.current] ? storages[currentStorageIndex.current].id : 'default'; useEffect(() => { @@ -39,6 +35,7 @@ export function LocationSelectContent(props) { // const formatedPath = path.normalize(newPath.replace(/\\/g, '/')); const formatedPath = path.normalize(newPath); await fetchFolderItemsByPath(storageId, formatedPath); + await actions.updateCurrentPath(formatedPath); setCurrentPath(formatedPath); } }; @@ -48,7 +45,7 @@ export function LocationSelectContent(props) { let path = currentPath; let id = ''; if (storages[index]) { - path = path || storages[index].path; + path = storages[index].path; id = storages[index].id; } updateCurrentPath(path, id); @@ -58,7 +55,6 @@ export function LocationSelectContent(props) { if (onChange) { onChange(currentPath); } - storage.set(NEW_BOT_LOCATION_KEY, currentPath); }, [currentPath]); const onSelectionChanged = item => { diff --git a/Composer/packages/client/src/store/action/storage.ts b/Composer/packages/client/src/store/action/storage.ts index 8cbb1f0cbc..13ef23ca4c 100644 --- a/Composer/packages/client/src/store/action/storage.ts +++ b/Composer/packages/client/src/store/action/storage.ts @@ -93,3 +93,7 @@ export const fetchFolderItemsByPath: ActionCreator = async ({ dispatch }, id, pa }); } }; + +export const updateCurrentPath: ActionCreator = async ({ dispatch }, path) => { + await httpClient.put(`/storages/currentPath`, { path: path }); +}; diff --git a/Composer/packages/server/__tests__/services/project.test.ts b/Composer/packages/server/__tests__/services/project.test.ts index cbca239796..1e89ca3385 100644 --- a/Composer/packages/server/__tests__/services/project.test.ts +++ b/Composer/packages/server/__tests__/services/project.test.ts @@ -16,6 +16,7 @@ jest.mock('../../src/store/store', () => { name: 'This PC', type: 'LocalDisk', path: '.', + defaultPath: '.', }, ], recentBotProjects: [] as any[], diff --git a/Composer/packages/server/__tests__/services/storage.test.ts b/Composer/packages/server/__tests__/services/storage.test.ts index 1e507c1157..b93b445482 100644 --- a/Composer/packages/server/__tests__/services/storage.test.ts +++ b/Composer/packages/server/__tests__/services/storage.test.ts @@ -24,6 +24,7 @@ jest.mock('../../src/store/store', () => { name: 'This PC', type: 'LocalDisk', path: '.', + defaultPath: '.', }, ]; return { diff --git a/Composer/packages/server/src/controllers/storage.ts b/Composer/packages/server/src/controllers/storage.ts index 33f1ee4515..a5249252ed 100644 --- a/Composer/packages/server/src/controllers/storage.ts +++ b/Composer/packages/server/src/controllers/storage.ts @@ -15,6 +15,11 @@ function createStorageConnection(req: Request, res: Response) { res.status(200).json(StorageService.getStorageConnections()); } +function updateCurrentPath(req: Request, res: Response) { + StorageService.updateCurrentPath(req.body.path); + res.status(200).json('success'); +} + async function getBlob(req: Request, res: Response) { const storageId = req.params.storageId; const reqpath = decodeURI(req.params.path); @@ -34,4 +39,5 @@ export const StorageController = { getStorageConnections, createStorageConnection, getBlob, + updateCurrentPath, }; diff --git a/Composer/packages/server/src/router/api.ts b/Composer/packages/server/src/router/api.ts index ac359258c3..4a829acc1a 100644 --- a/Composer/packages/server/src/router/api.ts +++ b/Composer/packages/server/src/router/api.ts @@ -32,6 +32,7 @@ router.post('/projects/opened/project/saveAs', ProjectController.saveProjectAs); router.get('/projects/recent', ProjectController.getRecentProjects); // storages +router.put('/storages/currentPath', StorageController.updateCurrentPath); router.get('/storages', StorageController.getStorageConnections); router.post('/storages', StorageController.createStorageConnection); router.get('/storages/:storageId/blobs/:path(*)', StorageController.getBlob); diff --git a/Composer/packages/server/src/services/storage.ts b/Composer/packages/server/src/services/storage.ts index 29c84cf788..d38ee66fab 100644 --- a/Composer/packages/server/src/services/storage.ts +++ b/Composer/packages/server/src/services/storage.ts @@ -18,6 +18,7 @@ class StorageService { constructor() { this.storageConnections = Store.get(this.STORE_KEY); + this.ensureDefaultBotFoldersExist(); } public getStorageClient = (storageId: string): IFileStorage => { @@ -39,11 +40,18 @@ class StorageService { }; public getStorageConnections = (): StorageConnection[] => { - return this.storageConnections.map(s => { + const connections = this.storageConnections.map(s => { const temp = Object.assign({}, s); - temp.path = Path.resolve(s.path); // resolve path if path is relative, and change it to unix pattern + // if the last accessed path exist + if (fs.existsSync(s.path)) { + temp.path = Path.resolve(s.path); // resolve path if path is relative, and change it to unix pattern + } else { + temp.path = Path.resolve(s.defaultPath); + } return temp; }); + this.ensureDefaultBotFoldersExist(); + return connections; }; public checkBlob = async (storageId: string, filePath: string): Promise => { @@ -85,6 +93,17 @@ class StorageService { } }; + public updateCurrentPath = (path: string) => { + this.storageConnections[0].path = path; + Store.set(this.STORE_KEY, this.storageConnections); + }; + + private ensureDefaultBotFoldersExist = () => { + this.storageConnections.forEach(s => { + this.createFolderRecurively(s.defaultPath); + }); + }; + private isBotFolder = (path: string) => { // locate Main.dialog const mainPath = Path.join(path, 'ComposerDialogs/Main', 'Main.dialog'); @@ -119,6 +138,13 @@ class StorageService { const result = await Promise.all(children); return result.filter(item => !!item); }; + + private createFolderRecurively = (path: string) => { + if (!fs.existsSync(path)) { + this.createFolderRecurively(Path.dirname(path)); + fs.mkdirSync(path); + } + }; } const service = new StorageService(); diff --git a/Composer/packages/server/src/store/data.template.ts b/Composer/packages/server/src/store/data.template.ts index 9bd5830cdc..b75e1d5f95 100644 --- a/Composer/packages/server/src/store/data.template.ts +++ b/Composer/packages/server/src/store/data.template.ts @@ -2,6 +2,7 @@ // Licensed under the MIT License. import path from 'path'; +import os from 'os'; export default { storageConnections: [ @@ -9,7 +10,8 @@ export default { id: 'default', name: 'This PC', type: 'LocalDisk', - path: path.resolve(__dirname, '../../../../../MyBots'), + path: '', // this is used as last accessed path, if it is invalid, use defaultPath + defaultPath: path.join(os.homedir(), 'Documents', 'Composer'), }, ], recentBotProjects: [],