Skip to content

Commit

Permalink
feat(office): Impled ability to download document from office and loa…
Browse files Browse the repository at this point in the history
…d to elastic
  • Loading branch information
breadrock1 committed Jun 21, 2024
1 parent 433d543 commit 9610933
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 3 deletions.
8 changes: 5 additions & 3 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ LOGGER_ENABLE_FILE_LOG=false
DOC_NOTIFIER_SERVICE_ADDRESS=0.0.0.0:2893
DOC_NOTIFIER_WATCHED_DIRS=./indexer/watcher

OCR_SERVICE_ADDRESS=http://localhost:1231
OCR_SERVICE_MODE=raw
OCR_SERVICE_ADDRESS=http://192.168.0.59:8004
OCR_SERVICE_MODE=assistant
OCR_SERVICE_TIMEOUT=300

DOC_SEARCHER_SERVICE_ADDRESS=http://localhost:2892
DOC_SEARCHER_SERVICE_TIMEOUT=300

TOKENIZER_SERVICE_ADDRESS=http://localhost:8001
TOKENIZER_SERVICE_ADDRESS=http://192.168.0.59:8082
TOKENIZER_SERVICE_MODE=none
TOKENIZER_RETURN_CHUNKS=false
TOKENIZER_CHUNK_SIZE=800
Expand All @@ -28,3 +28,5 @@ STORAGE_PORT=5432
STORAGE_DB_NAME=documents
STORAGE_ENABLE_SSL=disable
STORAGE_LLM_ADDRESS=http://localhost:8081

OFFICE_SERVICE_ADDRESS=http://localhost:8087/example
3 changes: 3 additions & 0 deletions cmd/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"doc-notifier/cmd"
"doc-notifier/internal/logger"
"doc-notifier/internal/ocr"
"doc-notifier/internal/office"
"doc-notifier/internal/reader"
"doc-notifier/internal/searcher"
"doc-notifier/internal/server"
Expand All @@ -26,6 +27,7 @@ func main() {
}

readService := reader.New()
officeService := office.New(&serviceConfig.Office)
ocrService := ocr.New(&serviceConfig.Ocr)
searchService := searcher.New(&serviceConfig.Searcher)
tokenService := tokenizer.New(&serviceConfig.Tokenizer)
Expand All @@ -37,6 +39,7 @@ func main() {
searchService,
tokenService,
storeService,
officeService,
)

ctx, cancel := context.WithCancel(context.Background())
Expand Down
3 changes: 3 additions & 0 deletions configs/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ Port=5432
DbName="documents"
EnableSSL="disable"
AddressLLM="http://localhost:8081"

[office]
Address="http://localhost:8087/example"
42 changes: 42 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,48 @@ const docTemplate = `{
}
}
},
"/watcher/files/upload-from-office": {
"post": {
"description": "Upload files to analyse from onlyoffice",
"produces": [
"application/json"
],
"tags": [
"files"
],
"summary": "Upload files to analyse from onlyoffice",
"operationId": "files-upload-office",
"parameters": [
{
"type": "string",
"description": "File name to download from office",
"name": "fileName",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "Ok",
"schema": {
"$ref": "#/definitions/server.ResponseForm"
}
},
"400": {
"description": "Bad Request message",
"schema": {
"$ref": "#/definitions/server.BadRequestForm"
}
},
"503": {
"description": "Server does not available",
"schema": {
"$ref": "#/definitions/server.ServerErrorForm"
}
}
}
}
},
"/watcher/folders/": {
"get": {
"description": "Get watched directories list",
Expand Down
42 changes: 42 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,48 @@
}
}
},
"/watcher/files/upload-from-office": {
"post": {
"description": "Upload files to analyse from onlyoffice",
"produces": [
"application/json"
],
"tags": [
"files"
],
"summary": "Upload files to analyse from onlyoffice",
"operationId": "files-upload-office",
"parameters": [
{
"type": "string",
"description": "File name to download from office",
"name": "fileName",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "Ok",
"schema": {
"$ref": "#/definitions/server.ResponseForm"
}
},
"400": {
"description": "Bad Request message",
"schema": {
"$ref": "#/definitions/server.BadRequestForm"
}
},
"503": {
"description": "Server does not available",
"schema": {
"$ref": "#/definitions/server.ServerErrorForm"
}
}
}
}
},
"/watcher/folders/": {
"get": {
"description": "Get watched directories list",
Expand Down
28 changes: 28 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,34 @@ paths:
summary: Upload files to analyse
tags:
- files
/watcher/files/upload-from-office:
post:
description: Upload files to analyse from onlyoffice
operationId: files-upload-office
parameters:
- description: File name to download from office
in: query
name: fileName
required: true
type: string
produces:
- application/json
responses:
"200":
description: Ok
schema:
$ref: '#/definitions/server.ResponseForm'
"400":
description: Bad Request message
schema:
$ref: '#/definitions/server.BadRequestForm'
"503":
description: Server does not available
schema:
$ref: '#/definitions/server.ServerErrorForm'
summary: Upload files to analyse from onlyoffice
tags:
- files
/watcher/folders/:
get:
description: Get watched directories list
Expand Down
5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Config struct {
Searcher SearcherConfig
Tokenizer TokenizerConfig
Storage StorageConfig
Office OfficeConfig
}

type LoggerConfig struct {
Expand Down Expand Up @@ -53,3 +54,7 @@ type StorageConfig struct {
EnableSSL string
AddressLLM string
}

type OfficeConfig struct {
Address string
}
4 changes: 4 additions & 0 deletions internal/config/from_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ func LoadEnv(enableDotenv bool) (*Config, error) {
AddressLLM: storageAddressLLM,
}

officeAddress := loadString("OFFICE_SERVICE_ADDRESS")
officeConfig := OfficeConfig{Address: officeAddress}

return &Config{
Logger: loggerConfig,
Watcher: watcherConfig,
Ocr: ocrConfig,
Searcher: searcherConfig,
Tokenizer: tokenizerConfig,
Storage: storageConfig,
Office: officeConfig,
}, nil
}

Expand Down
2 changes: 2 additions & 0 deletions internal/config/from_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func FromFile(filePath string) (*Config, error) {
viperInstance.SetDefault("tokenizer.ChunkBySelf", false)
viperInstance.SetDefault("tokenizer.Timeout", 300)

viperInstance.SetDefault("office.Address", "http://localhost:8087")

if err := viperInstance.ReadInConfig(); err != nil {
confErr := fmt.Errorf("failed while reading config file %s: %w", filePath, err)
return config, confErr
Expand Down
37 changes: 37 additions & 0 deletions internal/office/office.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package office

import (
"fmt"
"log"
"os"

"doc-notifier/internal/config"
"doc-notifier/internal/sender"
)

type Service struct {
address string
}

func New(config *config.OfficeConfig) *Service {
return &Service{
address: config.Address,
}
}

func (s *Service) DownloadDocument(fileName string) error {
targetUrl := fmt.Sprintf("%s/download?fileName=%s", s.address, fileName)
fileData, err := sender.SendGETRequest(targetUrl)
if err != nil {
log.Println("failed to download file from office: ")
return err
}

filePath := fmt.Sprintf("./indexer/watcher/%s", fileName)
err = os.WriteFile(filePath, fileData, os.ModePerm)
if err != nil {
log.Println("failed to write file: ", err)
}

return err
}
20 changes: 20 additions & 0 deletions internal/server/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (s *Service) CreateFilesGroup() error {
//})

group.POST("/upload", s.UploadFilesToUnrecognized)
group.POST("/upload-from-office", s.UploadFileFromOnlyOffice)
//group.POST("/analyse", s.AnalyseFiles, timeoutMW)

Check failure on line 23 in internal/server/files.go

View workflow job for this annotation

GitHub Actions / golangci

commentFormatting: put a space between `//` and comment text (gocritic)
group.GET("/artifacts", s.GetArtifactsTemplate)
group.POST("/analyse", s.AnalyseFiles)
Expand Down Expand Up @@ -67,6 +68,25 @@ func (s *Service) UploadFilesToUnrecognized(c echo.Context) error {
return c.JSON(200, uploadFiles)
}

// UploadFileFromOnlyOffice
// @Summary Upload files to analyse from onlyoffice
// @Description Upload files to analyse from onlyoffice
// @ID files-upload-office
// @Tags files
// @Produce json
// @Param fileName query string true "File name to download from office"
// @Success 200 {object} ResponseForm "Ok"
// @Failure 400 {object} BadRequestForm "Bad Request message"
// @Failure 503 {object} ServerErrorForm "Server does not available"
// @Router /watcher/files/upload-from-office [post]

Check failure on line 81 in internal/server/files.go

View workflow job for this annotation

GitHub Actions / golangci

Comment should end in a period (godot)
func (s *Service) UploadFileFromOnlyOffice(c echo.Context) error {
fileName := c.QueryParam("fileName")
if err := s.watcher.Office.DownloadDocument(fileName); err != nil {
return c.JSON(400, createStatusResponse(400, err.Error()))
}
return c.JSON(200, createStatusResponse(200, "Done"))
}

// AnalyseFiles
// @Summary Analyse uploaded files by ids
// @Description Analyse uploaded files by ids
Expand Down
4 changes: 4 additions & 0 deletions internal/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package watcher

import (
"context"
"doc-notifier/internal/office"
"errors"
"log"
"math"
Expand Down Expand Up @@ -37,6 +38,7 @@ type NotifyWatcher struct {
Searcher *searcher.Service
Tokenizer *tokenizer.Service
Storage *storage.Service
Office *office.Service
}

func New(
Expand All @@ -46,6 +48,7 @@ func New(
searcherService *searcher.Service,
tokenService *tokenizer.Service,
storageService *storage.Service,
officeService *office.Service,
) *NotifyWatcher {
notifyWatcher, err := fsnotify.NewWatcher()
if err != nil {
Expand All @@ -69,6 +72,7 @@ func New(
Searcher: searcherService,
Tokenizer: tokenService,
Storage: storageService,
Office: officeService,
}
}

Expand Down

0 comments on commit 9610933

Please sign in to comment.