Browse Source

add onlyoffice

master
godo 6 months ago
parent
commit
10b633ef6b
  1. 45
      frontend/src/components/window/OnlyOffice.vue
  2. 30
      frontend/src/components/window/WindowInner.vue
  3. 22
      godo/cmd/main.go
  4. 22
      godo/cmd/serve.go

45
frontend/src/components/window/OnlyOffice.vue

@ -7,7 +7,6 @@
import { DocumentEditor } from "@onlyoffice/document-editor-vue"; import { DocumentEditor } from "@onlyoffice/document-editor-vue";
import { getSystemConfig } from "@/system/config"; import { getSystemConfig } from "@/system/config";
import { BrowserWindow, Dialog, Notify, System } from "@/system"; import { BrowserWindow, Dialog, Notify, System } from "@/system";
import { generateRandomString } from "@/util/common";
const config = getSystemConfig(); const config = getSystemConfig();
const sys: any = inject<System>("system"); const sys: any = inject<System>("system");
const win: any = inject<BrowserWindow>("browserWindow"); const win: any = inject<BrowserWindow>("browserWindow");
@ -26,56 +25,26 @@ const props = defineProps({
}, },
}); });
const editorConfig: any = ref({}) const editorConfig: any = ref({})
// async function fetchDocumentKey(path: string): Promise<string> {
// try {
// const response:any = await fetch(`${config.onlyoffice.url}/get-document-key?path=${path}`);
// console.log(response)
// const res = await response.json();
// return res.data.key;
// } catch (error) {
// console.error("Failed to fetch document key:", error);
// throw error;
// }
// }
onMounted(() => { onMounted(() => {
const path = win?.config?.path; const path = win?.config?.path;
const uniqueKey = generateRandomString(12);
//const uniqueKey = fetchDocumentKey(path) //const uniqueKey = fetchDocumentKey(path)
const readUrl = config.apiUrl + "/file/readfile?stream=1&path=" + path const readUrl = config.apiUrl + "/file/readfile?stream=1&path=" + path
editorConfig.value = { editorConfig.value = {
document: { document: {
fileType: "docx", fileType: "docx",
//key: "ojR1OasBPnlIwF9WA80AW4NTrIWqs9",
//"key": uniqueKey,
key: "docx" + Math.random(), key: "docx" + Math.random(),
// "permissions": {
// "chat": true,
// "comment": true,
// "copy": true,
// "download": true,
// "edit": true,
// "fillForms": true,
// "modifyContentControl": true,
// "modifyFilter": true,
// "print": true,
// "review": true,
// "reviewGroups": null,
// "commentGroups": {},
// "userInfoGroups": null,
// "protect": true
// },
title: "Example Document Title.docx", title: "Example Document Title.docx",
url: readUrl url: readUrl
}, },
documentType: "word", documentType: "word",
editorConfig: { editorConfig: {
callbackUrl: "https://example.com/url-to-callback.ashx", //callbackUrl: "https://example.com/url-to-callback.ashx",
// customization: { customization: {
// "anonymous": { "anonymous": {
// request: true, request: true,
// label: "Guest", label: "Guest",
// } }
// }, },
} }
} }
}) })

30
frontend/src/components/window/WindowInner.vue

@ -1,11 +1,15 @@
<template> <template>
<template v-if="win.url"> <template v-if="win.url">
<IframeFile v-if="config.editorType =='local'" :src="win.url" :ext="win.ext" :eventType="win.eventType" /> <IframeFile v-if="config.editorType == 'local'" :src="win.url" :ext="win.ext" :eventType="win.eventType" />
<OnlyOffice v-else :src="win.url" :eventType="win.eventType" :ext="ext" /> <template v-if="officeFile">
<OnlyOffice v-if="config.editorType == 'onlyoffice'" :src="win.url" :eventType="win.eventType" :ext="ext" />
</template>
<IframeFile v-else :src="win.url" :ext="win.ext" :eventType="win.eventType" />
</template> </template>
<Suspense v-else> <Suspense v-else>
<component v-if="win.content" :is="stepComponent(win.content)" :translateSavePath="translateSavePath" :componentID="win.windowInfo.componentID"></component> <component v-if="win.content" :is="stepComponent(win.content)" :translateSavePath="translateSavePath"
:componentID="win.windowInfo.componentID"></component>
<RouterView v-else /> <RouterView v-else />
</Suspense> </Suspense>
<!-- <component :is="window.content"></component> --> <!-- <component :is="window.content"></component> -->
@ -19,16 +23,26 @@ const router = useRouter();
const props = defineProps<{ const props = defineProps<{
win: any; win: any;
}>(); }>();
// const word = ['doc', 'docm', 'docx', 'docxf', 'dot', 'dotm', 'dotx', 'epub', 'fodt', 'fb2', 'htm', 'html', 'mht', 'odt', 'oform', 'ott', 'oxps', 'pdf', 'rtf', 'txt', 'djvu', 'xml', 'xps'];
// const cell = ['csv', 'fods', 'ods', 'ots', 'xls', 'xlsb', 'xlsm', 'xlsx', 'xlt', 'xltm', 'xltx'];
// const slide = ['fodp', 'odp', 'otp', 'pot', 'potm', 'potx', 'pps', 'ppsm', 'ppsx', 'ppt', 'pptm', 'pptx']
let ext = "txt" let ext = "txt"
const win = ref(props.win) const win = ref(props.win)
if(win.value.config.path){ const officeFile = ref(false)
if (win.value.config.path) {
ext = win.value.config.path.split('.').pop() ext = win.value.config.path.split('.').pop()
officeFile.value = isOffice(ext)
} }
const translateSavePath = inject('translateSavePath') const translateSavePath = inject('translateSavePath')
if (props.win.path) { if (props.win.path) {
router.push(props.win.path); router.push(props.win.path);
} }
function isOffice(ext: string) {
const word = ['doc', 'docm', 'docx', 'docxf', 'dot', 'dotm', 'dotx', 'epub', 'fodt', 'fb2', 'mht', 'odt', 'oform', 'ott', 'oxps', 'pdf', 'rtf', 'txt', 'djvu', 'xml', 'xps'];
const cell = ['csv', 'fods', 'ods', 'ots', 'xls', 'xlsb', 'xlsm', 'xlsx', 'xlt', 'xltm', 'xltx'];
const slide = ['fodp', 'odp', 'otp', 'pot', 'potm', 'potx', 'pps', 'ppsm', 'ppsx', 'ppt', 'pptm', 'pptx']
if (word.includes(ext) || cell.includes(ext) || slide.includes(ext)) {
return true
} else {
return false
}
}
</script> </script>

22
godo/cmd/main.go

@ -31,7 +31,6 @@ import (
"io/fs" "io/fs"
"log" "log"
"net/http" "net/http"
"time"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -51,7 +50,6 @@ func OsStart() {
router := mux.NewRouter() router := mux.NewRouter()
router.Use(recoverMiddleware) router.Use(recoverMiddleware)
router.Use(corsMiddleware()) router.Use(corsMiddleware())
// 使用带有日志装饰的处理器注册路由
router.Use(loggingMiddleware{}.Middleware) router.Use(loggingMiddleware{}.Middleware)
staticDir := libs.GetStaticDir() staticDir := libs.GetStaticDir()
router.PathPrefix("/static").Handler(http.StripPrefix("/static", http.FileServer(http.Dir(staticDir)))) router.PathPrefix("/static").Handler(http.StripPrefix("/static", http.FileServer(http.Dir(staticDir))))
@ -152,7 +150,7 @@ func OsStart() {
distFS, _ := fs.Sub(deps.Frontendassets, "dist") distFS, _ := fs.Sub(deps.Frontendassets, "dist")
fileServer := http.FileServer(http.FS(distFS)) fileServer := http.FileServer(http.FS(distFS))
netPath := libs.GetNetPath() netPath := libs.GetNetPath()
log.Printf("netPath: %s", netPath) //log.Printf("netPath: %s", netPath)
if netPath != "/" { if netPath != "/" {
// 注册 netPath 的处理函数 // 注册 netPath 的处理函数
@ -169,21 +167,3 @@ func OsStart() {
srv = &http.Server{Addr: serverAddress, Handler: router} srv = &http.Server{Addr: serverAddress, Handler: router}
Serve(srv) Serve(srv)
} }
func OsStop() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := store.StopAllHandler()
if err != nil {
log.Fatalf("Servers forced to shutdown error: %v", err)
}
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("Server forced to shutdown: %v", err)
}
log.Println("Server stopped.")
}
func OsRestart() {
// 停止当前服务
OsStop()
// 重新启动服务
OsStart()
}

22
godo/cmd/serve.go

@ -22,6 +22,7 @@ import (
"context" "context"
"fmt" "fmt"
"godo/libs" "godo/libs"
"godo/store"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -33,6 +34,25 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
func OsStop() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := store.StopAllHandler()
if err != nil {
log.Fatalf("Servers forced to shutdown error: %v", err)
}
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("Server forced to shutdown: %v", err)
}
log.Println("Server stopped.")
}
func OsRestart() {
// 停止当前服务
OsStop()
// 重新启动服务
OsStart()
}
func Serve(srv *http.Server) { func Serve(srv *http.Server) {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
@ -95,7 +115,7 @@ func recoverMiddleware(next http.Handler) http.Handler {
// CORS 中间件 // CORS 中间件
func corsMiddleware() mux.MiddlewareFunc { func corsMiddleware() mux.MiddlewareFunc {
allowHeaders := "Content-Type, Accept, Authorization, Origin,Pwd" allowHeaders := "Content-Type, Accept, Authorization, Origin, Pwd"
allowMethods := "GET, POST, PUT, DELETE, OPTIONS" allowMethods := "GET, POST, PUT, DELETE, OPTIONS"
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {

Loading…
Cancel
Save