Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't error on unknown files #180

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,570 changes: 1,288 additions & 1,282 deletions api/docs/index.html

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,10 @@ definitions:
type: object
service.SnapshotDetachOptions:
properties:
fileID:
fileId:
type: string
required:
- fileID
- fileId
type: object
service.SnapshotList:
properties:
Expand Down Expand Up @@ -3173,6 +3173,24 @@ paths:
summary: Get Count
tags:
- Tasks
/tasks/dismiss:
post:
consumes:
- application/json
description: Dismiss All
operationId: tasks_dismiss_all
produces:
- application/json
responses:
"200":
description: OK
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/errorpkg.ErrorResponse'
summary: Dismiss All
tags:
- Tasks
/users:
get:
description: List
Expand Down
6 changes: 3 additions & 3 deletions api/repo/task_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (s *taskEntity) SetPayload(p map[string]string) {
type TaskRepo interface {
Insert(opts TaskInsertOptions) (model.Task, error)
Find(id string) (model.Task, error)
GetIDs() ([]string, error)
GetIDs(userID string) ([]string, error)
GetCount(email string) (int64, error)
Save(task model.Task) error
Delete(id string) error
Expand Down Expand Up @@ -225,12 +225,12 @@ func (repo *taskRepo) Find(id string) (model.Task, error) {
return res, nil
}

func (repo *taskRepo) GetIDs() ([]string, error) {
func (repo *taskRepo) GetIDs(userID string) ([]string, error) {
type Value struct {
Result string
}
var values []Value
db := repo.db.Raw("SELECT id result FROM task ORDER BY create_time DESC").Scan(&values)
db := repo.db.Raw("SELECT id result FROM task WHERE user_id = ? ORDER BY create_time DESC", userID).Scan(&values)
if db.Error != nil {
return []string{}, db.Error
}
Expand Down
20 changes: 20 additions & 0 deletions api/router/task_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewTaskRouter() *TaskRouter {
func (r *TaskRouter) AppendRoutes(g fiber.Router) {
g.Get("/", r.List)
g.Get("/count", r.GetCount)
g.Post("/dismiss", r.DismissAll)
g.Get("/:id", r.Get)
g.Post("/:id/dismiss", r.Dismiss)
}
Expand Down Expand Up @@ -169,6 +170,25 @@ func (r *TaskRouter) Dismiss(c *fiber.Ctx) error {
return c.SendStatus(http.StatusNoContent)
}

// DismissAll godoc
//
// @Summary Dismiss All
// @Description Dismiss All
// @Tags Tasks
// @Id tasks_dismiss_all
// @Accept json
// @Produce json
// @Success 200
// @Failure 500 {object} errorpkg.ErrorResponse
// @Router /tasks/dismiss [post]
func (r *TaskRouter) DismissAll(c *fiber.Ctx) error {
userID := GetUserID(c)
if err := r.taskSvc.DismissAll(userID); err != nil {
return err
}
return c.SendStatus(http.StatusNoContent)
}

// Create godoc
//
// @Summary Create
Expand Down
25 changes: 22 additions & 3 deletions api/service/task_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ type TaskList struct {
func (svc *TaskService) List(opts TaskListOptions, userID string) (*TaskList, error) {
var authorized []model.Task
if opts.Query == "" {
ids, err := svc.taskRepo.GetIDs()
ids, err := svc.taskRepo.GetIDs(userID)
if err != nil {
return nil, err
}
Expand All @@ -175,11 +175,11 @@ func (svc *TaskService) List(opts TaskListOptions, userID string) (*TaskList, er
return nil, err
}
} else {
orgs, err := svc.taskSearch.Query(opts.Query)
tasks, err := svc.taskSearch.Query(opts.Query)
if err != nil {
return nil, err
}
authorized, err = svc.doAuthorization(orgs, userID)
authorized, err = svc.doAuthorization(tasks, userID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -307,6 +307,25 @@ func (svc *TaskService) Dismiss(id string, userID string) error {
return svc.deleteAndSync(id)
}

func (svc *TaskService) DismissAll(userID string) error {
ids, err := svc.taskRepo.GetIDs(userID)
if err != nil {
return err
}
authorized, err := svc.doAuthorizationByIDs(ids, userID)
if err != nil {
return err
}
for _, t := range authorized {
if t.HasError() {
if err := svc.deleteAndSync(t.GetID()); err != nil {
return err
}
}
}
return nil
}

func (svc *TaskService) Delete(id string) error {
return svc.deleteAndSync(id)
}
Expand Down
2 changes: 1 addition & 1 deletion conversion/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PORT=8083

DISABLE_INSTALLER=false
ENABLE_INSTALLER=false

# URLs
API_URL="http://127.0.0.1:8080"
Expand Down
22 changes: 11 additions & 11 deletions conversion/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (
)

type Config struct {
Port int
APIURL string
LanguageURL string
MosaicURL string
DisableInstaller bool
Security SecurityConfig
Limits LimitsConfig
S3 S3Config
Port int
APIURL string
LanguageURL string
MosaicURL string
EnableInstaller bool
Security SecurityConfig
Limits LimitsConfig
S3 S3Config
}

type SecurityConfig struct {
Expand Down Expand Up @@ -56,12 +56,12 @@ func GetConfig() *Config {
config = &Config{
Port: port,
}
if len(os.Getenv("DISABLE_INSTALLER")) > 0 {
v, err := strconv.ParseBool(os.Getenv("DISABLE_INSTALLER"))
if len(os.Getenv("ENABLE_INSTALLER")) > 0 {
v, err := strconv.ParseBool(os.Getenv("ENABLE_INSTALLER"))
if err != nil {
panic(err)
}
config.DisableInstaller = v
config.EnableInstaller = v
}
readURLs(config)
readSecurity(config)
Expand Down
1 change: 1 addition & 0 deletions conversion/identifier/file_identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (fi *FileIdentifier) IsPlainText(path string) bool {
".yaml",
".toml",
".md",
".csv",
}
extension := filepath.Ext(path)
for _, v := range extensions {
Expand Down
2 changes: 1 addition & 1 deletion conversion/pipeline/audio_video_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (p *audioVideoPipeline) Run(opts client.PipelineRunOptions) error {
return err
}
// Here we intentionally ignore the error, as the media file may contain just audio
p.createThumbnail(inputPath, opts)
_ = p.createThumbnail(inputPath, opts)
if err := p.apiClient.PatchTask(opts.TaskID, client.TaskPatchOptions{
Fields: []string{client.TaskFieldName},
Name: helper.ToPtr("Saving preview."),
Expand Down
19 changes: 0 additions & 19 deletions conversion/pipeline/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
package pipeline

import (
"errors"

"github.com/kouprlabs/voltaserve/conversion/client"
"github.com/kouprlabs/voltaserve/conversion/errorpkg"
"github.com/kouprlabs/voltaserve/conversion/helper"
Expand Down Expand Up @@ -81,23 +79,6 @@ func (d *Dispatcher) Dispatch(opts client.PipelineRunOptions) error {
err = d.glbPipeline.Run(opts)
} else if id == model.PipelineZIP {
err = d.zipPipeline.Run(opts)
} else {
if err := d.apiClient.PatchSnapshot(client.SnapshotPatchOptions{
Options: opts,
Fields: []string{client.SnapshotFieldStatus},
Status: helper.ToPtr(client.SnapshotStatusError),
}); err != nil {
return err
}
err = errors.New("no matching pipeline found")
if err := d.apiClient.PatchTask(opts.TaskID, client.TaskPatchOptions{
Fields: []string{client.TaskFieldStatus, client.TaskFieldError},
Status: helper.ToPtr(client.TaskStatusError),
Error: helper.ToPtr(errorpkg.GetUserFriendlyMessage(err.Error(), errorpkg.FallbackMessage)),
}); err != nil {
return err
}
return err
}
if err != nil {
if err := d.apiClient.PatchSnapshot(client.SnapshotPatchOptions{
Expand Down
5 changes: 2 additions & 3 deletions conversion/pipeline/glb_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ func (p *glbPipeline) createThumbnail(inputPath string, opts client.PipelineRunO
infra.GetLogger().Error(err)
}
}(tmpPath)
if err := p.glbProc.Thumbnail(inputPath, p.config.Limits.ImagePreviewMaxWidth, p.config.Limits.ImagePreviewMaxHeight, "rgb(255,255,255)", tmpPath); err != nil {
return err
}
// We don't consider failing the creation of the thumbnail as an error
_ = p.glbProc.Thumbnail(inputPath, p.config.Limits.ImagePreviewMaxWidth, p.config.Limits.ImagePreviewMaxHeight, "rgb(255,255,255)", tmpPath)
props, err := p.imageProc.MeasureImage(tmpPath)
if err != nil {
return err
Expand Down
19 changes: 9 additions & 10 deletions conversion/pipeline/image_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,21 @@ func (p *imagePipeline) Run(opts client.PipelineRunOptions) error {
return err
}
}
defer func(path string) {
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(imagePath)
if err := p.apiClient.PatchTask(opts.TaskID, client.TaskPatchOptions{
Fields: []string{client.TaskFieldName},
Name: helper.ToPtr("Creating thumbnail."),
}); err != nil {
return err
}
if err := p.createThumbnail(imagePath, opts); err != nil {
return err
}
// We don't consider failing the creation of the thumbnail as an error
_ = p.createThumbnail(imagePath, opts)
if err := p.apiClient.PatchTask(opts.TaskID, client.TaskPatchOptions{
Fields: []string{client.TaskFieldName, client.TaskFieldStatus},
Name: helper.ToPtr("Done."),
Expand Down Expand Up @@ -177,13 +183,6 @@ func (p *imagePipeline) convertTIFFToJPEG(inputPath string, imageProps client.Im
if err := p.imageProc.ConvertImage(inputPath, jpegPath); err != nil {
return nil, err
}
defer func(path string) {
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
} else if err != nil {
infra.GetLogger().Error(err)
}
}(jpegPath)
stat, err := os.Stat(jpegPath)
if err != nil {
return nil, err
Expand Down
5 changes: 2 additions & 3 deletions conversion/pipeline/pdf_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ func (p *pdfPipeline) Run(opts client.PipelineRunOptions) error {

func (p *pdfPipeline) createThumbnail(inputPath string, opts client.PipelineRunOptions) error {
tmpPath := filepath.FromSlash(os.TempDir() + "/" + helper.NewID() + ".png")
if err := p.pdfProc.Thumbnail(inputPath, 0, p.config.Limits.ImagePreviewMaxHeight, tmpPath); err != nil {
return err
}
// We don't consider failing the creation of the thumbnail as an error
_ = p.pdfProc.Thumbnail(inputPath, 0, p.config.Limits.ImagePreviewMaxHeight, tmpPath)
defer func(path string) {
if err := os.Remove(path); errors.Is(err, os.ErrNotExist) {
return
Expand Down
2 changes: 1 addition & 1 deletion conversion/runtime/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (d *Installer) IsRunning() bool {
func (d *Installer) Start() {
go func() {
d.isRunning = true
if !d.config.DisableInstaller {
if d.config.EnableInstaller {
d.installCoreTools()
d.installGltfPipeline()
d.installScreenshotGLB()
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ services:
- ${VOLTASERVE_CONVERSION_PORT}:8083
environment:
- PORT=8083
- ENABLE_INSTALLER=true
- API_URL=http://api:8080
- LANGUAGE_URL=http://language:8084
- MOSAIC_URL=http://mosaic:8085
Expand Down
8 changes: 7 additions & 1 deletion ui/src/client/api/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// the Business Source License, use of this software will be governed
// by the GNU Affero General Public License v3.0 only, included in the file
// licenses/AGPL.txt.

import useSWR, { SWRConfiguration } from 'swr'
import { apiFetcher } from '@/client/fetcher'

Expand Down Expand Up @@ -136,4 +135,11 @@ export default class TaskAPI {
method: 'POST',
})
}

static async dismissAll() {
return apiFetcher({
url: `/tasks/dismiss`,
method: 'POST',
})
}
}
Loading