From af1fedeeefa646d8ea73d8d517e16f18e8942ba5 Mon Sep 17 00:00:00 2001 From: godo Date: Tue, 27 Aug 2024 08:24:06 +0800 Subject: [PATCH] change init type --- CHANGELOG.md | 13 +- .../src/components/computer/QuickLink.vue | 16 +- frontend/src/components/desktop/DeskItem.vue | 1 - frontend/src/components/desktop/Upgrade.vue | 4 +- frontend/src/components/store/Store.vue | 2 +- frontend/src/components/taskbar/Magnet.vue | 2 +- frontend/src/hook/useAppOpen.ts | 5 +- frontend/src/stores/upgrade.ts | 55 +- frontend/src/system/config.ts | 2 +- frontend/src/system/index.ts | 131 ++-- frontend/src/system/initBuiltin.ts | 6 +- godo/cmd/main.go | 6 + godo/deps/desktop.json | 589 ++++++++++++++++++ godo/files/destop.go | 126 ++++ godo/files/fs.go | 25 +- godo/files/init.go | 231 +++++++ godo/libs/info.go | 57 +- godo/sys/update.go | 2 +- 18 files changed, 1141 insertions(+), 132 deletions(-) create mode 100644 godo/deps/desktop.json create mode 100644 godo/files/destop.go create mode 100644 godo/files/init.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 3982c01..58a1b1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,13 @@ ## 变更记录 +- 2024-08-27 +1. 优化初始化系统,初始化系统时只请求读写一次 +2. 去除浏览器存储 - 2024-08-21 -内网聊天增加手工添加ip,跨网段通信在ping通的前提下如果发现不了对方可手工添加对方ip -修复思维导图保存的文件每次打开主题又会变成默认主题 -新增webdav客户端 -新增远程存储 -修改选择文件夹会删除文件夹内的文件 +1. 内网聊天增加手工添加ip,跨网段通信在ping通的前提下如果发现不了对方可手工添加对方ip +2. 修复思维导图保存的文件每次打开主题又会变成默认主题 +3. 新增webdav客户端 +4. 新增远程存储 +5. 修改选择文件夹会删除文件夹内的文件 - 2024-08-07 1. 新增web端安装 diff --git a/frontend/src/components/computer/QuickLink.vue b/frontend/src/components/computer/QuickLink.vue index e5d43c9..8ad11e1 100644 --- a/frontend/src/components/computer/QuickLink.vue +++ b/frontend/src/components/computer/QuickLink.vue @@ -34,42 +34,42 @@ const quickLinks = [ { img: "word", name: t("document"), - path: "/C/Users/Doc", + path: "/C/Users/Documents/Word", }, { img: "excel", name: t("excel"), - path: "/C/Users/Execl", + path: "/C/Users/Documents/Execl", }, { img: "pptexe", name: t("ppt"), - path: "/C/Users/PPT", + path: "/C/Users/Documents/PPT", }, { img: "markdown", name: t("markdown"), - path: "/C/Users/Markdown", + path: "/C/Users/Documents/Markdown", }, { img: "mindexe", name: t("mindmap"), - path: "/C/Users/Mind", + path: "/C/Users/Documents/Mind", }, { img: "baiban", name: t("whiteBoard"), - path: "/C/Users/Baiban", + path: "/C/Users/Documents/Baiban", }, { img: "kanban", name: t("board"), - path: "/C/Users/Kanban", + path: "/C/Users/Documents/Kanban", }, { img: "gallery", name: t("gallery"), - path: "/C/Users/Photo", + path: "/C/Users/Pictures", }, { img: "music", diff --git a/frontend/src/components/desktop/DeskItem.vue b/frontend/src/components/desktop/DeskItem.vue index 89fb922..cc09e00 100644 --- a/frontend/src/components/desktop/DeskItem.vue +++ b/frontend/src/components/desktop/DeskItem.vue @@ -12,7 +12,6 @@ import { mountEvent } from "@/system/event"; import { useSystem } from "@/system/index.ts"; import { useAppOpen } from "@/hook/useAppOpen"; import { onMounted } from "vue"; - const { openapp, appList } = useAppOpen("apps"); const props = defineProps({ onChosen: { diff --git a/frontend/src/components/desktop/Upgrade.vue b/frontend/src/components/desktop/Upgrade.vue index adbb4f9..ae3ebbe 100644 --- a/frontend/src/components/desktop/Upgrade.vue +++ b/frontend/src/components/desktop/Upgrade.vue @@ -17,8 +17,8 @@
GodoOS {{ upgradeStore.upgradeDesc }}
- - CHANGELOG.md + + 查看详情
{{ $t('upgrade.desc') }}
diff --git a/frontend/src/components/store/Store.vue b/frontend/src/components/store/Store.vue index c799b90..a7ce702 100644 --- a/frontend/src/components/store/Store.vue +++ b/frontend/src/components/store/Store.vue @@ -53,7 +53,7 @@ async function install(item: any) { async function uninstall(item: any) { await store.removeDesktop(item); - if (item.needInstall) { + if (item.haStart) { const completion = await fetch(store.apiUrl + '/store/uninstall?name=' + item.name) if (!completion.ok) { notifyError(t("store.hasSameName")) diff --git a/frontend/src/components/taskbar/Magnet.vue b/frontend/src/components/taskbar/Magnet.vue index 806f8de..e4eef14 100644 --- a/frontend/src/components/taskbar/Magnet.vue +++ b/frontend/src/components/taskbar/Magnet.vue @@ -24,7 +24,7 @@ import { OsFileWithoutContent } from "@/system/core/FileSystem"; import { vGlowing } from "@/util/glowingBorder"; import { t } from "@/i18n"; -const { openapp, appList } = useAppOpen("magnet"); +const { openapp, appList } = useAppOpen("menulist"); function handle(item: OsFileWithoutContent) { emitEvent("magnet.item.click", item); openapp(item); diff --git a/frontend/src/hook/useAppOpen.ts b/frontend/src/hook/useAppOpen.ts index e072efd..3ab886d 100644 --- a/frontend/src/hook/useAppOpen.ts +++ b/frontend/src/hook/useAppOpen.ts @@ -2,9 +2,12 @@ import { OsFileWithoutContent } from '../system/core/FileSystem'; import { useSystem } from '../system'; function useAppOpen(type: 'apps' | 'magnet' | 'menulist') { - const rootState = useSystem()._rootState; const system = useSystem(); + const rootState = system._rootState; + const appList = rootState[type]; + //console.log(appList) + //console.log(appList) function openapp(item: OsFileWithoutContent) { //console.log(item) diff --git a/frontend/src/stores/upgrade.ts b/frontend/src/stores/upgrade.ts index 4b405de..9ee7c8d 100644 --- a/frontend/src/stores/upgrade.ts +++ b/frontend/src/stores/upgrade.ts @@ -9,31 +9,60 @@ export const useUpgradeStore = defineStore('upgradeStore', () => { const hasNotice = ref(false); const hasAd = ref(false); const updateUrl = ref(''); - const versionTag = ref(0) + const versionTag = ref('') const upgradeDesc = ref('') - const currentVersion = ref(0) + const currentVersion = ref('') const progress = ref(0) const noticeList:any = ref([]) const adList:any = ref([]) + function compareVersions(version1:string, version2:string) { + // 将版本号字符串按"."分割成数组 + const parts1 = version1.split('.').map(Number); + const parts2 = version2.split('.').map(Number); + + // 确保两个数组长度相同 + const maxLength = Math.max(parts1.length, parts2.length); + while (parts1.length < maxLength) parts1.push(0); + while (parts2.length < maxLength) parts2.push(0); + + // 比较每个部分 + for (let i = 0; i < maxLength; i++) { + if (parts1[i] > parts2[i]) return 1; + if (parts1[i] < parts2[i]) return -1; + } + + // 如果所有部分都相等,则返回0 + return 0; + } async function checkUpdate() { const config = getSystemConfig(); currentVersion.value = config.version; const releaseRes = await fetch(`${config.apiUrl}/system/updateInfo`) if (!releaseRes.ok) return; const releaseData = await releaseRes.json() - versionTag.value = releaseData.version - if(versionTag.value > config.version){ - upgradeDesc.value = releaseData.desc ?? t('upgrade.msg') - hasUpgrade.value = true - updateUrl.value = releaseData.url + if(releaseData.code !== 200){ + return } - if (releaseData.noticeList && releaseData.noticeList.length > 0) { - hasNotice.value = true - noticeList.value = releaseData.noticeList + const res = releaseData.data + if(!res.version || res.version == ""){ + return } - if (!hasUpgrade.value && releaseData.adList && releaseData.adList.length > 0) { - hasAd.value = true - adList.value = releaseData.adList + versionTag.value = res.version + if (compareVersions(versionTag.value, config.version) > 0) { + upgradeDesc.value = res.desc ?? t('upgrade.msg') + hasUpgrade.value = true + updateUrl.value = res.url + } + if (res.adList) { + if(res.adList.center && res.adList.center.length > 0){ + hasNotice.value = true + noticeList.value = res.adList.center + } + if(res.adList.bottom && res.adList.bottom.length > 0){ + hasAd.value = true + adList.value = res.adList.bottom + } + } } async function update() { diff --git a/frontend/src/system/config.ts b/frontend/src/system/config.ts index ae6405d..eb13f84 100644 --- a/frontend/src/system/config.ts +++ b/frontend/src/system/config.ts @@ -1,5 +1,5 @@ import { generateRandomString } from "../util/common.ts" -export const configStoreType = localStorage.getItem('GodoOS-storeType') || 'browser'; +export const configStoreType = localStorage.getItem('GodoOS-storeType') || 'local'; /** * 获取系统配置信息。 * 从本地存储中获取或初始化系统配置对象,并根据条件决定是否更新本地存储中的配置。 diff --git a/frontend/src/system/index.ts b/frontend/src/system/index.ts index f5b9831..4c145ae 100644 --- a/frontend/src/system/index.ts +++ b/frontend/src/system/index.ts @@ -10,7 +10,7 @@ import { } from './type/type'; import { initEventer, Eventer, initEventListener } from './event'; import { OsFileSystem } from './core/FileSystem'; -import {useOsFile} from './core/FileOs'; +import { useOsFile } from './core/FileOs'; import { version } from '../../package.json'; import { BrowserWindow, BrowserWindowOption } from './window/BrowserWindow'; @@ -22,9 +22,7 @@ import { Notify, NotifyConstructorOptions } from './notification/Notification'; import { Dialog } from './window/Dialog'; import { pick } from '../util/modash'; import { Tray, TrayOptions } from './menu/Tary'; -import { InitSystemFile, InitUserFile } from './core/SystemFileConfig'; -import { createInitFile } from './core/createInitFile'; -import { getSystemConfig, getSystemKey, setSystemKey, setSystemConfig, clearSystemConfig } from './config' +import { getSystemConfig, getSystemKey, setSystemKey, setSystemConfig, clearSystemConfig, getFileUrl } from './config' import { useUpgradeStore } from '@/stores/upgrade'; import { RestartApp } from '@/util/goutil'; @@ -64,13 +62,13 @@ export class System { fs!: any; constructor(options?: SystemOptions) { - + this._options = this.initOptions(options); this._rootState = this.initRootState(); System.GLOBAL_SYSTEM = this; // 挂载全局系统 Bios._onOpen && Bios._onOpen(this); this._eventer = this.initEvent(); - this.firstRun(); + this.firstRun(); this.initSystem(); } @@ -100,8 +98,8 @@ export class System { await this.initFileSystem(); // 初始化文件系统 await this.initSavedConfig(); // 初始化保存的配置 initBuiltinApp(this); // 初始化内建应用 - this.initApp(); // 初始化配置应用到app文件夹中 - this.initAppList(); // 刷新app文件夹,展示应用 + //this.initApp(); // 初始化配置应用到app文件夹中 + //this.initAppList(); // 刷新app文件夹,展示应用 // 判断是否登录 this.isLogin(); initEventListener(); // 初始化事件侦听 @@ -110,21 +108,10 @@ export class System { //this.runPlugin(this); // 运行fs中插件 this.initBackground(); // 初始化壁纸 this.emit('start'); - + setTimeout(() => { - if (this._rootState.magnet?.length < 1) { - this.refershAppList() - } - setTimeout(() => { - if (this._rootState.magnet?.length < 1) { - //this.recover() - clearSystemConfig(); - RestartApp(); - return; - } - const upgradeStore = useUpgradeStore(); - upgradeStore.checkUpdate() - }, 3000); + const upgradeStore = useUpgradeStore(); + upgradeStore.checkUpdate() }, 6000); } @@ -164,7 +151,7 @@ export class System { const background = getSystemKey('background'); if (background.type === 'image') { this._rootState.options.background = background.url - }else{ + } else { this._rootState.options.background = background.color; } // this._rootState.options.background = '/image/bg/bg5.jpg'; @@ -179,37 +166,63 @@ export class System { return initEventer(); } private initApp() { - - this._rootState.options.desktop?.forEach((item) => { - this.addApp(item); - }); - this._rootState.options.magnet?.forEach((item) => { - this.addMagnet(item); - }); - this._rootState.options.menulist?.forEach((item) => { - this.addMenuList(item); - }); + const fileUrl = getFileUrl(); + if (!fileUrl) return; + fetch(`${fileUrl}/desktop`).then(res => res.json()).then(res => { + if (res && res.code == 0) { + this._rootState.apps = res.data.apps; + this._rootState.menulist = res.data.menulist; + res.data.apps.forEach((item: any) => { + this._rootState.windowMap['Desktop'].set(item.name, item); + }) + res.data.apps.forEach((item: any) => { + this._rootState.windowMap['Menulist'].set(item.name, item); + }) + // this._rootState.windowMap[loc].set(options.name, options); + // useSystem()._rootState.apps = res.data.apps; + // useSystem()._rootState.menulist = res.data.menulist; + } + }) + // this._rootState.options.desktop?.forEach((item) => { + // this.addApp(item); + // }); + // this._rootState.options.menulist?.forEach((item) => { + // this.addMenuList(item); + // }); } refershAppList() { - const APP_TYPE = ['apps', 'magnet', 'menulist']; + const system = useSystem(); + if (!system) return; + const fileUrl = getFileUrl(); + if (!fileUrl) return; + fetch(`${fileUrl}/desktop`).then(res => res.json()).then(res => { + if (res && res.code == 0) { + // system._rootState.apps = res.data.apps; + // system._rootState.menulist = res.data.menulist; + system._rootState.apps.splice(0, system._rootState.apps.length, ...res.data.apps); + system._rootState.menulist.splice(0, system._rootState.menulist.length, ...res.data.menulist); + } + }) + //const APP_TYPE = ['apps', 'menulist']; + // console.log('refresh') + /* for (let i = 0; i < APP_TYPE.length; i++) { const element = APP_TYPE[i]; system?.fs .readdir( `${system._options.userLocation}${{ apps: 'Desktop', - magnet: 'Magnet', menulist: 'Menulist', }[element] }` ) - .then((res:any) => { + .then((res: any) => { if (res) { const list = res; - const tempList:any = []; + const tempList: any = []; for (let j = 0; j < list.length; j++) { const item = list[j]; @@ -236,6 +249,7 @@ export class System { } }); } + */ } isReadyUpdateAppList = false; @@ -250,38 +264,12 @@ export class System { } private async initFileSystem() { - const storeType = getSystemKey("storeType") - if(storeType == 'browser'){ - this.fs = await new OsFileSystem().initFileSystem(this._options); - (this.fs as OsFileSystem).on('error', (err: string) => { - this.emitError(err); - }); - this.fs.registerWatcher(new RegExp(`^${this._options.userLocation}`), () => { - this.initAppList(); - }); - }else{ - //this.fs = this._options.fs; - this.fs = useOsFile(); - await this.initOutSystem() - } + this.fs = useOsFile(); + // this.fs.registerWatcher(new RegExp(`^${this._options.userLocation}`), () => { + // this.initAppList(); + // }); } - async initOutSystem() { - if (!this.isFirstRun) { - return; - } - await this.fs.mkdir('/C'); - await this.fs.mkdir('/D'); - await this.fs.mkdir('/E'); - await this.fs.mkdir('/B'); - await createInitFile(this.fs, InitUserFile, this._options.userLocation); - await createInitFile(this.fs, InitSystemFile, this._options.systemLocation); - - // await this.fs.chmod('/C', OsFileMode.ReadWrite); - // await this.fs.chmod('/D', OsFileMode.ReadWrite); - // await this.fs.chmod('/E', OsFileMode.ReadWrite); - // await this.fs.chmod('/B', OsFileMode.ReadWrite); - } replaceFileSystem(fs: OsFileInterface) { this.fs = fs; this.initAppList(); @@ -293,7 +281,7 @@ export class System { console.error('自定义文件系统不支持挂载卷'); } } - + /** * @description: 初始化保存的配置 */ @@ -360,9 +348,6 @@ export class System { addApp(options: WinAppOptions, force = false) { this.addWindowSysLink('Desktop', options, force); } - addMagnet(options: WinAppOptions, force = false) { - this.addWindowSysLink('Magnet', options, force); - } addMenuList(options: WinAppOptions, force = false) { this.addWindowSysLink('Menulist', options, force); } @@ -399,7 +384,7 @@ export class System { this._rootState.state = SystemStateEnum.close; this.fs.removeFileSystem().then(() => { window.indexedDB.deleteDatabase("GodoDatabase"); - + RestartApp(); }) @@ -412,7 +397,7 @@ export class System { } emitEvent(event: string, ...args: any[]) { const eventArray = event.split('.'); - eventArray.forEach((_:any, index) => { + eventArray.forEach((_: any, index) => { const tempEvent = eventArray.slice(0, index + 1).join('.'); this._eventer.emit(tempEvent, event, args); }); diff --git a/frontend/src/system/initBuiltin.ts b/frontend/src/system/initBuiltin.ts index b7be985..6d178c7 100644 --- a/frontend/src/system/initBuiltin.ts +++ b/frontend/src/system/initBuiltin.ts @@ -35,9 +35,9 @@ export function initBuiltinApp(system: System) { if (d.isDeskTop) { system.addApp(addSave); } - if (d.isMagnet) { - system.addMagnet(addSave); - } + // if (d.isMagnet) { + // system.addMagnet(addSave); + // } if (d.isMenuList) { system.addMenuList(addSave); } diff --git a/godo/cmd/main.go b/godo/cmd/main.go index 79144f1..f6fe353 100644 --- a/godo/cmd/main.go +++ b/godo/cmd/main.go @@ -23,6 +23,11 @@ var srv *http.Server func OsStart() { libs.InitServer() + err := files.InitOsSystem() + if err != nil { + log.Fatalf("InitOsSystem error: %v", err) + return + } webdav.InitWebdav() router := mux.NewRouter() router.Use(corsMiddleware()) @@ -54,6 +59,7 @@ func OsStart() { router.HandleFunc("/system/update", sys.UpdateAppHandler).Methods(http.MethodGet) router.HandleFunc("/system/setting", sys.ConfigHandler).Methods(http.MethodPost) fileRouter := router.PathPrefix("/file").Subrouter() + fileRouter.HandleFunc("/desktop", files.HandleDesktop).Methods(http.MethodGet) fileRouter.HandleFunc("/info", files.HandleSystemInfo).Methods(http.MethodGet) fileRouter.HandleFunc("/read", files.HandleReadDir).Methods(http.MethodGet) fileRouter.HandleFunc("/stat", files.HandleStat).Methods(http.MethodGet) diff --git a/godo/deps/desktop.json b/godo/deps/desktop.json new file mode 100644 index 0000000..d9473e7 --- /dev/null +++ b/godo/deps/desktop.json @@ -0,0 +1,589 @@ +{ + "updatetime": "2024-08-24T10:42:22.086Z", + "apps": [ + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 32, + "mtime": "2024-08-24T10:42:22.086Z", + "atime": "2024-08-24T10:42:22.086Z", + "birthtime": "2024-08-24T10:42:22.086Z", + "mode": 511, + "rdev": 0, + "name": "computer.exe", + "path": "/C/Users/Desktop/computer.exe", + "oldPath": "/C/Users/Desktop/computer.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::computer::diannao", + "ext": "exe", + "title": "computer", + "isSys": 1, + "id": 28 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 30, + "mtime": "2024-08-24T10:42:22.095Z", + "atime": "2024-08-24T10:42:22.095Z", + "birthtime": "2024-08-24T10:42:22.095Z", + "mode": 511, + "rdev": 0, + "name": "appstore.exe", + "path": "/C/Users/Desktop/appstore.exe", + "oldPath": "/C/Users/Desktop/appstore.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::appstore::store", + "ext": "exe", + "title": "appstore", + "isSys": 1, + "id": 31 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 29, + "mtime": "2024-08-24T10:42:22.101Z", + "atime": "2024-08-24T10:42:22.101Z", + "birthtime": "2024-08-24T10:42:22.101Z", + "mode": 511, + "rdev": 0, + "name": "document.exe", + "path": "/C/Users/Desktop/document.exe", + "oldPath": "/C/Users/Desktop/document.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::document::word", + "ext": "exe", + "title": "document", + "isSys": 1, + "id": 38 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 27, + "mtime": "2024-08-24T10:42:22.102Z", + "atime": "2024-08-24T10:42:22.102Z", + "birthtime": "2024-08-24T10:42:22.102Z", + "mode": 511, + "rdev": 0, + "name": "excel.exe", + "path": "/C/Users/Desktop/excel.exe", + "oldPath": "/C/Users/Desktop/excel.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::excel::excel", + "ext": "exe", + "title": "excel", + "isSys": 1, + "id": 39 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 33, + "mtime": "2024-08-24T10:42:22.102Z", + "atime": "2024-08-24T10:42:22.102Z", + "birthtime": "2024-08-24T10:42:22.102Z", + "mode": 511, + "rdev": 0, + "name": "markdown.exe", + "path": "/C/Users/Desktop/markdown.exe", + "oldPath": "/C/Users/Desktop/markdown.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::markdown::markdown", + "ext": "exe", + "title": "markdown", + "isSys": 1, + "id": 40 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 31, + "mtime": "2024-08-24T10:42:22.104Z", + "atime": "2024-08-24T10:42:22.104Z", + "birthtime": "2024-08-24T10:42:22.104Z", + "mode": 511, + "rdev": 0, + "name": "mindmap.exe", + "path": "/C/Users/Desktop/mindmap.exe", + "oldPath": "/C/Users/Desktop/mindmap.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::mindmap::mindexe", + "ext": "exe", + "title": "mindmap", + "isSys": 1, + "id": 41 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 26, + "mtime": "2024-08-24T10:42:22.105Z", + "atime": "2024-08-24T10:42:22.105Z", + "birthtime": "2024-08-24T10:42:22.105Z", + "mode": 511, + "rdev": 0, + "name": "ppt.exe", + "path": "/C/Users/Desktop/ppt.exe", + "oldPath": "/C/Users/Desktop/ppt.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::ppt::pptexe", + "ext": "exe", + "title": "ppt", + "isSys": 1, + "id": 42 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 35, + "mtime": "2024-08-24T10:42:22.105Z", + "atime": "2024-08-24T10:42:22.105Z", + "birthtime": "2024-08-24T10:42:22.105Z", + "mode": 511, + "rdev": 0, + "name": "fileEditor.exe", + "path": "/C/Users/Desktop/fileEditor.exe", + "oldPath": "/C/Users/Desktop/fileEditor.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::fileEditor::editorbt", + "ext": "exe", + "title": "fileEditor", + "isSys": 1, + "id": 43 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 28, + "mtime": "2024-08-24T10:42:22.105Z", + "atime": "2024-08-24T10:42:22.105Z", + "birthtime": "2024-08-24T10:42:22.105Z", + "mode": 511, + "rdev": 0, + "name": "board.exe", + "path": "/C/Users/Desktop/board.exe", + "oldPath": "/C/Users/Desktop/board.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::board::kanban", + "ext": "exe", + "title": "board", + "isSys": 1, + "id": 44 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 33, + "mtime": "2024-08-24T10:42:22.106Z", + "atime": "2024-08-24T10:42:22.106Z", + "birthtime": "2024-08-24T10:42:22.106Z", + "mode": 511, + "rdev": 0, + "name": "whiteBoard.exe", + "path": "/C/Users/Desktop/whiteBoard.exe", + "oldPath": "/C/Users/Desktop/whiteBoard.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::whiteBoard::baiban", + "ext": "exe", + "title": "whiteBoard", + "isSys": 1, + "id": 45 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 30, + "mtime": "2024-08-24T10:42:22.107Z", + "atime": "2024-08-24T10:42:22.107Z", + "birthtime": "2024-08-24T10:42:22.107Z", + "mode": 511, + "rdev": 0, + "name": "localchat.exe", + "path": "/C/Users/Desktop/localchat.exe", + "oldPath": "/C/Users/Desktop/localchat.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::localchat::chat", + "ext": "exe", + "title": "localchat", + "isSys": 1, + "id": 46 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 33, + "mtime": "2024-08-24T10:42:22.108Z", + "atime": "2024-08-24T10:42:22.108Z", + "birthtime": "2024-08-24T10:42:22.108Z", + "mode": 511, + "rdev": 0, + "name": "piceditor.exe", + "path": "/C/Users/Desktop/piceditor.exe", + "oldPath": "/C/Users/Desktop/piceditor.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::piceditor::picedit", + "ext": "exe", + "title": "piceditor", + "isSys": 1, + "id": 47 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 26, + "mtime": "2024-08-24T10:42:22.109Z", + "atime": "2024-08-24T10:42:22.109Z", + "birthtime": "2024-08-24T10:42:22.109Z", + "mode": 511, + "rdev": 0, + "name": "gantt.exe", + "path": "/C/Users/Desktop/gantt.exe", + "oldPath": "/C/Users/Desktop/gantt.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::gantt::gant", + "ext": "exe", + "title": "gantt", + "isSys": 1, + "id": 48 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 30, + "mtime": "2024-08-24T10:42:22.112Z", + "atime": "2024-08-24T10:42:22.112Z", + "birthtime": "2024-08-24T10:42:22.112Z", + "mode": 511, + "rdev": 0, + "name": "browser.exe", + "path": "/C/Users/Desktop/browser.exe", + "oldPath": "/C/Users/Desktop/browser.exe", + "parentPath": "/C/Users/Desktop", + "content": "link::Desktop::browser::brower", + "ext": "exe", + "title": "browser", + "isSys": 1, + "id": 53 + } + ], + "magnet": [ + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 30, + "mtime": "2024-08-24T10:42:22.083Z", + "atime": "2024-08-24T10:42:22.083Z", + "birthtime": "2024-08-24T10:42:22.083Z", + "mode": 511, + "rdev": 0, + "name": "setting.exe", + "path": "/C/Users/Magnet/setting.exe", + "oldPath": "/C/Users/Magnet/setting.exe", + "parentPath": "/C/Users/Magnet", + "content": "link::Magnet::setting::setting", + "ext": "exe", + "title": "setting", + "isSys": 1, + "id": 26 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 31, + "mtime": "2024-08-24T10:42:22.090Z", + "atime": "2024-08-24T10:42:22.090Z", + "birthtime": "2024-08-24T10:42:22.090Z", + "mode": 511, + "rdev": 0, + "name": "computer.exe", + "path": "/C/Users/Magnet/computer.exe", + "oldPath": "/C/Users/Magnet/computer.exe", + "parentPath": "/C/Users/Magnet", + "content": "link::Magnet::computer::diannao", + "ext": "exe", + "title": "computer", + "isSys": 1, + "id": 29 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 29, + "mtime": "2024-08-24T10:42:22.096Z", + "atime": "2024-08-24T10:42:22.096Z", + "birthtime": "2024-08-24T10:42:22.096Z", + "mode": 511, + "rdev": 0, + "name": "appstore.exe", + "path": "/C/Users/Magnet/appstore.exe", + "oldPath": "/C/Users/Magnet/appstore.exe", + "parentPath": "/C/Users/Magnet", + "content": "link::Magnet::appstore::store", + "ext": "exe", + "title": "appstore", + "isSys": 1, + "id": 32 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 34, + "mtime": "2024-08-24T10:42:22.097Z", + "atime": "2024-08-24T10:42:22.097Z", + "birthtime": "2024-08-24T10:42:22.097Z", + "mode": 511, + "rdev": 0, + "name": "system.version.exe", + "path": "/C/Users/Magnet/system.version.exe", + "oldPath": "/C/Users/Magnet/system.version.exe", + "parentPath": "/C/Users/Magnet", + "content": "link::Magnet::system.version::info", + "ext": "exe", + "title": "system.version", + "isSys": 1, + "id": 34 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 37, + "mtime": "2024-08-24T10:42:22.099Z", + "atime": "2024-08-24T10:42:22.099Z", + "birthtime": "2024-08-24T10:42:22.099Z", + "mode": 511, + "rdev": 0, + "name": "process.title.exe", + "path": "/C/Users/Magnet/process.title.exe", + "oldPath": "/C/Users/Magnet/process.title.exe", + "parentPath": "/C/Users/Magnet", + "content": "link::Magnet::process.title::progress", + "ext": "exe", + "title": "process.title", + "isSys": 1, + "id": 36 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 36, + "mtime": "2024-08-24T10:42:22.110Z", + "atime": "2024-08-24T10:42:22.110Z", + "birthtime": "2024-08-24T10:42:22.110Z", + "mode": 511, + "rdev": 0, + "name": "calculator.exe", + "path": "/C/Users/Magnet/calculator.exe", + "oldPath": "/C/Users/Magnet/calculator.exe", + "parentPath": "/C/Users/Magnet", + "content": "link::Magnet::calculator::calculator", + "ext": "exe", + "title": "calculator", + "isSys": 1, + "id": 49 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 32, + "mtime": "2024-08-24T10:42:22.111Z", + "atime": "2024-08-24T10:42:22.111Z", + "birthtime": "2024-08-24T10:42:22.111Z", + "mode": 511, + "rdev": 0, + "name": "calendar.exe", + "path": "/C/Users/Magnet/calendar.exe", + "oldPath": "/C/Users/Magnet/calendar.exe", + "parentPath": "/C/Users/Magnet", + "content": "link::Magnet::calendar::calendar", + "ext": "exe", + "title": "calendar", + "isSys": 1, + "id": 50 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 31, + "mtime": "2024-08-24T10:42:22.111Z", + "atime": "2024-08-24T10:42:22.111Z", + "birthtime": "2024-08-24T10:42:22.111Z", + "mode": 511, + "rdev": 0, + "name": "musicStore.exe", + "path": "/C/Users/Magnet/musicStore.exe", + "oldPath": "/C/Users/Magnet/musicStore.exe", + "parentPath": "/C/Users/Magnet", + "content": "link::Magnet::musicStore::music", + "ext": "exe", + "title": "musicStore", + "isSys": 1, + "id": 51 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 30, + "mtime": "2024-08-24T10:42:22.112Z", + "atime": "2024-08-24T10:42:22.112Z", + "birthtime": "2024-08-24T10:42:22.112Z", + "mode": 511, + "rdev": 0, + "name": "gallery.exe", + "path": "/C/Users/Magnet/gallery.exe", + "oldPath": "/C/Users/Magnet/gallery.exe", + "parentPath": "/C/Users/Magnet", + "content": "link::Magnet::gallery::gallery", + "ext": "exe", + "title": "gallery", + "isSys": 1, + "id": 52 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 29, + "mtime": "2024-08-24T10:42:22.113Z", + "atime": "2024-08-24T10:42:22.113Z", + "birthtime": "2024-08-24T10:42:22.113Z", + "mode": 511, + "rdev": 0, + "name": "browser.exe", + "path": "/C/Users/Magnet/browser.exe", + "oldPath": "/C/Users/Magnet/browser.exe", + "parentPath": "/C/Users/Magnet", + "content": "link::Magnet::browser::brower", + "ext": "exe", + "title": "browser", + "isSys": 1, + "id": 54 + } + ], + "menulist": [ + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 32, + "mtime": "2024-08-24T10:42:22.085Z", + "atime": "2024-08-24T10:42:22.085Z", + "birthtime": "2024-08-24T10:42:22.085Z", + "mode": 511, + "rdev": 0, + "name": "setting.exe", + "path": "/C/Users/Menulist/setting.exe", + "oldPath": "/C/Users/Menulist/setting.exe", + "parentPath": "/C/Users/Menulist", + "content": "link::Menulist::setting::setting", + "ext": "exe", + "title": "setting", + "isSys": 1, + "id": 27 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 33, + "mtime": "2024-08-24T10:42:22.094Z", + "atime": "2024-08-24T10:42:22.094Z", + "birthtime": "2024-08-24T10:42:22.094Z", + "mode": 511, + "rdev": 0, + "name": "computer.exe", + "path": "/C/Users/Menulist/computer.exe", + "oldPath": "/C/Users/Menulist/computer.exe", + "parentPath": "/C/Users/Menulist", + "content": "link::Menulist::computer::diannao", + "ext": "exe", + "title": "computer", + "isSys": 1, + "id": 30 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 31, + "mtime": "2024-08-24T10:42:22.096Z", + "atime": "2024-08-24T10:42:22.096Z", + "birthtime": "2024-08-24T10:42:22.096Z", + "mode": 511, + "rdev": 0, + "name": "appstore.exe", + "path": "/C/Users/Menulist/appstore.exe", + "oldPath": "/C/Users/Menulist/appstore.exe", + "parentPath": "/C/Users/Menulist", + "content": "link::Menulist::appstore::store", + "ext": "exe", + "title": "appstore", + "isSys": 1, + "id": 33 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 36, + "mtime": "2024-08-24T10:42:22.098Z", + "atime": "2024-08-24T10:42:22.098Z", + "birthtime": "2024-08-24T10:42:22.098Z", + "mode": 511, + "rdev": 0, + "name": "system.version.exe", + "path": "/C/Users/Menulist/system.version.exe", + "oldPath": "/C/Users/Menulist/system.version.exe", + "parentPath": "/C/Users/Menulist", + "content": "link::Menulist::system.version::info", + "ext": "exe", + "title": "system.version", + "isSys": 1, + "id": 35 + }, + { + "isFile": true, + "isDirectory": false, + "isSymlink": false, + "size": 39, + "mtime": "2024-08-24T10:42:22.100Z", + "atime": "2024-08-24T10:42:22.100Z", + "birthtime": "2024-08-24T10:42:22.100Z", + "mode": 511, + "rdev": 0, + "name": "process.title.exe", + "path": "/C/Users/Menulist/process.title.exe", + "oldPath": "/C/Users/Menulist/process.title.exe", + "parentPath": "/C/Users/Menulist", + "content": "link::Menulist::process.title::progress", + "ext": "exe", + "title": "process.title", + "isSys": 1, + "id": 37 + } + ] +} \ No newline at end of file diff --git a/godo/files/destop.go b/godo/files/destop.go new file mode 100644 index 0000000..04148cd --- /dev/null +++ b/godo/files/destop.go @@ -0,0 +1,126 @@ +package files + +import ( + "strings" + "time" +) + +type AppInfo struct { + Name string `json:"name"` + Path string `json:"path"` + OldPath string `json:"oldPath"` + ParentPath string `json:"parentPath"` + Content string `json:"content"` + Ext string `json:"ext"` + Title string `json:"title"` + IsSys int `json:"isSys"` + ID int `json:"id"` + IsFile bool `json:"isFile"` + IsDirectory bool `json:"isDirectory"` + IsSymlink bool `json:"isSymlink"` + Size int `json:"size"` + Mtime time.Time `json:"mtime"` // Modified time + Atime time.Time `json:"atime"` // Access time + Birthtime time.Time `json:"birthtime"` // Creation time + Mode int `json:"mode"` + Rdev int `json:"rdev"` +} + +// RootObject represents the root object in the JSON data. +type RootObject struct { + UpdateTime time.Time `json:"updatetime"` + Desktop []AppInfo `json:"apps"` + Menulist []AppInfo `json:"menulist"` +} + +var RootAppList = []map[string]string{ + {"name": "computer", "icon": "diannao", "position": "Desktop,Menulist"}, + {"name": "appstore", "icon": "store", "position": "Desktop,Menulist"}, + {"name": "localchat", "icon": "chat", "position": "Desktop"}, + {"name": "document", "icon": "word", "position": "Desktop"}, + {"name": "excel", "icon": "excel", "position": "Desktop"}, + {"name": "markdown", "icon": "markdown", "position": "Desktop"}, + {"name": "mindmap", "icon": "mindexe", "position": "Desktop"}, + {"name": "ppt", "icon": "pptexe", "position": "Desktop"}, + {"name": "fileEditor", "icon": "editorbt", "position": "Desktop"}, + {"name": "board", "icon": "kanban", "position": "Desktop"}, + {"name": "whiteBoard", "icon": "baiban", "position": "Desktop"}, + {"name": "piceditor", "icon": "picedit", "position": "Desktop"}, + {"name": "gantt", "icon": "gant", "position": "Desktop"}, + {"name": "browser", "icon": "brower", "position": "Desktop,Menulist"}, + {"name": "setting", "icon": "setting", "position": "Menulist"}, + {"name": "system.version", "icon": "info", "position": "Menulist"}, + {"name": "process.title", "icon": "progress", "position": "Menulist"}, + {"name": "calculator", "icon": "calculator", "position": "Menulist"}, + {"name": "calendar", "icon": "calendar", "position": "Menulist"}, + {"name": "musicStore", "icon": "music", "position": "Menulist"}, + {"name": "gallery", "icon": "gallery", "position": "Menulist"}, + {"name": "process.title", "icon": "progress", "position": "Menulist"}, +} + +// GetInitRootList constructs the initial root list. +func GetInitRootList() RootObject { + var desktopApps []AppInfo + var menulistApps []AppInfo + nowtime := time.Now() + var id = 1 + for _, app := range RootAppList { + positions := strings.Split(app["position"], ",") + content := "link::Desktop::" + app["name"] + "::" + app["icon"] + for _, pos := range positions { + switch pos { + case "Desktop": + newApp := AppInfo{ + Name: app["name"] + ".exe", + Path: "/C/Users/Desktop/" + app["name"] + ".exe", + OldPath: "/C/Users/Desktop/" + app["name"] + ".exe", + ParentPath: "/C/Users/Desktop", + Content: content, + Ext: "exe", + Title: app["name"], + IsSys: 1, + ID: id, + IsFile: true, + IsDirectory: false, + IsSymlink: false, + Size: len(content), // Size can be set to any value + Mtime: nowtime, + Atime: nowtime, + Birthtime: nowtime, + Mode: 511, + Rdev: 0, + } + desktopApps = append(desktopApps, newApp) + case "Menulist": + newApp := AppInfo{ + Name: app["name"] + ".exe", + Path: "/C/Users/Menulist/" + app["name"] + ".exe", + OldPath: "/C/Users/Menulist/" + app["name"] + ".exe", + ParentPath: "/C/Users/Menulist", + Content: content, + Ext: "exe", + Title: app["name"], + IsSys: 1, + ID: id, + IsFile: true, + IsDirectory: false, + IsSymlink: false, + Size: len(content), + Mtime: nowtime, + Atime: nowtime, + Birthtime: nowtime, + Mode: 511, + Rdev: 0, + } + menulistApps = append(menulistApps, newApp) + } + } + id++ + } + + return RootObject{ + UpdateTime: nowtime, + Desktop: desktopApps, + Menulist: menulistApps, + } +} diff --git a/godo/files/fs.go b/godo/files/fs.go index 51c5246..56ff9c2 100644 --- a/godo/files/fs.go +++ b/godo/files/fs.go @@ -192,16 +192,21 @@ func HandleUnlink(w http.ResponseWriter, r *http.Request) { // HandleClear removes the entire filesystem (Caution: Use with care!) func HandleClear(w http.ResponseWriter, r *http.Request) { - basePath, err := libs.GetOsDir() + // basePath, err := libs.GetOsDir() + // if err != nil { + // libs.HTTPError(w, http.StatusInternalServerError, err.Error()) + // return + // } + // err = Clear(basePath) + // if err != nil { + // libs.HTTPError(w, http.StatusConflict, err.Error()) + // return + // } + err := RecoverOsSystem() if err != nil { libs.HTTPError(w, http.StatusInternalServerError, err.Error()) return } - err = Clear(basePath) - if err != nil { - libs.HTTPError(w, http.StatusConflict, err.Error()) - return - } res := libs.APIResponse{Message: "FileSystem successfully cleared."} json.NewEncoder(w).Encode(res) } @@ -457,3 +462,11 @@ func parseMode(modeStr string) (os.FileMode, error) { } return os.FileMode(mode), nil } +func HandleDesktop(w http.ResponseWriter, r *http.Request) { + rootInfo, err := GetDesktop() + if err != nil { + libs.HTTPError(w, http.StatusInternalServerError, err.Error()) + return + } + libs.SuccessMsg(w, rootInfo, "success") +} diff --git a/godo/files/init.go b/godo/files/init.go new file mode 100644 index 0000000..6bddb9d --- /dev/null +++ b/godo/files/init.go @@ -0,0 +1,231 @@ +package files + +import ( + "encoding/json" + "fmt" + "godo/libs" + "os" + "path/filepath" + "time" +) + +func RecoverOsSystem() error { + basePath, err := libs.GetOsDir() + if err != nil { + return fmt.Errorf("GetOsDir error: %v", err) + } + + osCpath := filepath.Join(basePath, "C") + if libs.PathExists(osCpath) { + err := os.RemoveAll(osCpath) + if err != nil { + return fmt.Errorf("RemoveAll error: %v", err) + } + } + err = InitOsSystem() + if err != nil { + return fmt.Errorf("InitOsSystem error: %v", err) + } + return nil +} +func InitOsSystem() error { + basePath, err := libs.GetOsDir() + if err != nil { + return fmt.Errorf("GetOsDir error: %v", err) + } + if !libs.PathExists(basePath) { + if err := os.MkdirAll(basePath, 0755); err != nil { + return fmt.Errorf("Mkdir error: %v", err) + } + } + osCpath := filepath.Join(basePath, "C") + if !libs.PathExists(osCpath) { + if err := os.MkdirAll(osCpath, 0755); err != nil { + return fmt.Errorf("Mkdir error: %v", err) + } + baseOsDir := []string{ + "D", + "E", + "B", + } + for _, dir := range baseOsDir { + dirPath := filepath.Join(basePath, dir) + if !libs.PathExists(dirPath) { + if err := os.MkdirAll(dirPath, 0755); err != nil { + return fmt.Errorf("Mkdir error: %v", err) + } + } + } + systemPath := filepath.Join(osCpath, "System") + if !libs.PathExists(systemPath) { + if err := os.MkdirAll(systemPath, 0755); err != nil { + return fmt.Errorf("Mkdir error: %v", err) + } + } + userPath := filepath.Join(osCpath, "Users") + if !libs.PathExists(userPath) { + if err := os.MkdirAll(userPath, 0755); err != nil { + return fmt.Errorf("Mkdir error: %v", err) + } + InitPaths := []string{ + "Desktop", + "Menulist", + "Documents", + "Downloads", + "Music", + "Pictures", + "Videos", + "Schedule", + "Reciv", + } + for _, dir := range InitPaths { + dirPath := filepath.Join(userPath, dir) + if !libs.PathExists(dirPath) { + if err := os.MkdirAll(dirPath, 0755); err != nil { + return fmt.Errorf("Mkdir error: %v", err) + } + } + } + InitDocPath := []string{ + "Word", + "Markdown", + "PPT", + "Baiban", + "Kanban", + "Execl", + "Mind", + "Screenshot", + "ScreenRecording", + } + docpath := filepath.Join(userPath, "Documents") + for _, dir := range InitDocPath { + dirPath := filepath.Join(docpath, dir) + if !libs.PathExists(dirPath) { + if err := os.MkdirAll(dirPath, 0755); err != nil { + return fmt.Errorf("Mkdir error: %v", err) + } + } + } + + applist := GetInitRootList() + desktopPath := filepath.Join(userPath, "Desktop") + for _, app := range applist.Desktop { + appPath := filepath.Join(desktopPath, app.Name) + if !libs.PathExists(appPath) { + if err := os.WriteFile(appPath, []byte(app.Content), 0644); err != nil { + return fmt.Errorf("failed to write to file: %w", err) + } + } + } + menulistPath := filepath.Join(userPath, "Menulist") + for _, app := range applist.Menulist { + appPath := filepath.Join(menulistPath, app.Name) + if !libs.PathExists(appPath) { + if err := os.WriteFile(appPath, []byte(app.Content), 0644); err != nil { + return fmt.Errorf("failed to write to file: %w", err) + } + } + } + content, err := json.MarshalIndent(applist, "", " ") + if err != nil { + return fmt.Errorf("failed to marshal reqBodies to JSON: %w", err) + } + appPath, err := libs.GetAppDir() + if err != nil { + return fmt.Errorf("GetAppDir error: %v", err) + } + desktopFilePath := filepath.Join(appPath, "desktop.json") + if err := os.WriteFile(desktopFilePath, content, 0644); err != nil { + return fmt.Errorf("failed to write to file: %w", err) + } + } + + } + + return nil +} +func GetDesktop() (RootObject, error) { + var applist RootObject + desktoppath, err := GetDeskTopPath() + if err != nil || !libs.PathExists(desktoppath) { + return applist, fmt.Errorf("desktop.json not found") + + } + content, err := os.ReadFile(desktoppath) + if err != nil { + return applist, fmt.Errorf("failed to read file: %w", err) + } + + err = json.Unmarshal(content, &applist) + if err != nil { + return applist, fmt.Errorf("failed to unmarshal JSON: %w", err) + } + return applist, nil +} +func GetDeskTopPath() (string, error) { + appPath, err := libs.GetAppDir() + if err != nil { + return "", fmt.Errorf("GetAppDir error: %v", err) + } + return filepath.Join(appPath, "desktop.json"), nil +} +func WriteDesktop(rootInfo RootObject) error { + rootInfo.UpdateTime = time.Now() + content, err := json.MarshalIndent(rootInfo, "", " ") + if err != nil { + return fmt.Errorf("failed to marshal reqBodies to JSON: %w", err) + } + basePath, err := libs.GetOsDir() + if err != nil { + return fmt.Errorf("GetOsDir error: %v", err) + } + desktoppath := filepath.Join(basePath, "C", "System", "desktop.json") + return os.WriteFile(desktoppath, content, 0644) +} +func AddDesktop(app AppInfo, position string) error { + applist, err := GetDesktop() + if err != nil { + return fmt.Errorf("GetDesktop error: %v", err) + } + if position == "Desktop" { + if !checkHasApp(applist.Desktop, app) { + applist.Desktop = append(applist.Desktop, app) + } + } else if position == "Menulist" { + if !checkHasApp(applist.Menulist, app) { + applist.Menulist = append(applist.Menulist, app) + } + } else { + return fmt.Errorf("position error") + } + return WriteDesktop(applist) +} +func DeleteDesktop(name string) error { + rootInfo, err := GetDesktop() + if err != nil { + return fmt.Errorf("GetDesktop error: %v", err) + } + indexToDelete := -1 + for i, item := range rootInfo.Desktop { + if item.Name == name { + indexToDelete = i + break + } + } + // 如果找到了要删除的元素 + if indexToDelete >= 0 { + // 使用 append 删除元素 + rootInfo.Desktop = append(rootInfo.Desktop[:indexToDelete], rootInfo.Desktop[indexToDelete+1:]...) + } else { + return fmt.Errorf("item with name '%s' not found", name) + } + return WriteDesktop(rootInfo) +} +func checkHasApp(list []AppInfo, app AppInfo) bool { + for _, item := range list { + if item.Name == app.Name { + return true + } + } + return false +} diff --git a/godo/libs/info.go b/godo/libs/info.go index f2735de..f9709c3 100644 --- a/godo/libs/info.go +++ b/godo/libs/info.go @@ -59,23 +59,52 @@ func GenerateSystemInfo() UserOsInfo { // GetIPAddress 获取本机IP地址 func GetIPAddress() (string, error) { - addrs, err := net.InterfaceAddrs() + ips, err := GetValidIPAddresses() + //log.Printf("ips: %v", ips) if err != nil { return "", err } - for _, addr := range addrs { - var ip net.IP - switch v := addr.(type) { - case *net.IPNet: - ip = v.IP - case *net.IPAddr: - ip = v.IP + for _, ipStr := range ips { + ip := net.ParseIP(ipStr) + if ip != nil && ip.To4() != nil && ip.String()[:7] == "192.168" { + return ipStr, nil } - if ip != nil && !ip.IsLoopback() && ip.To4() != nil { - return ip.String(), nil + } + return "", fmt.Errorf("no valid IP addresses found") +} + +// GetValidIPAddresses 获取所有有效 IP 地址 +func GetValidIPAddresses() ([]string, error) { + var validIPs []string + ifaces, err := net.Interfaces() + if err != nil { + return nil, err + } + for _, iface := range ifaces { + addrs, err := iface.Addrs() + if err != nil { + continue + } + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP + } + if ip != nil && !ip.IsLoopback() && ip.To4() != nil { + // 过滤链路本地地址 + if !ip.IsLinkLocalUnicast() { + validIPs = append(validIPs, ip.String()) + } + } } } - return "", fmt.Errorf("no valid IP address found") + if len(validIPs) == 0 { + return nil, fmt.Errorf("no valid non-private and non-link-local IP address found") + } + return validIPs, nil } // getMACAddress 获取MAC地址 @@ -107,12 +136,8 @@ func GetSystemInfo() (string, error) { if !ok { return "", fmt.Errorf("未找到osInfo配置") } - systemInfo, ok := lineseInfo.(UserOsInfo) - if !ok { - return "", fmt.Errorf("osInfo配置错误") - } // 将系统信息序列化为JSON字符串 - jsonBytes, err := json.Marshal(systemInfo) + jsonBytes, err := json.Marshal(lineseInfo) if err != nil { return "", err } diff --git a/godo/sys/update.go b/godo/sys/update.go index 0251e43..2f5ae33 100644 --- a/godo/sys/update.go +++ b/godo/sys/update.go @@ -134,7 +134,7 @@ func GetUpdateUrlHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, "update error:"+err.Error(), http.StatusInternalServerError) return } - updateUrl := "https://godocms.com/version?info=" + info + updateUrl := "https://godoos.com/version?info=" + info res, err := http.Get(updateUrl) if err != nil { libs.ErrorMsg(w, err.Error())