/
admin-datasource-rethumb.go
115 lines (100 loc) · 3.87 KB
/
admin-datasource-rethumb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package cmd
import (
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/anypb"
"os"
"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/client/grpc"
"github.com/pydio/cells/v4/common/proto/jobs"
"github.com/pydio/cells/v4/common/proto/service"
"github.com/pydio/cells/v4/common/proto/tree"
"github.com/pydio/cells/v4/common/utils/uuid"
)
var (
rethumbDsName string
rethumbUserName string
rethumbMaxConcurrency int
rethumbTimeout string
)
var dsRethumbCmd = &cobra.Command{
Use: "rethumb",
Short: "Find and compute missing thumbnails for images",
Long: `
DESCRIPTION
Look up for files where x-cells-hash is missing and recompute them. This operation is launched in scheduler and can take
some time (and CPU).
EXAMPLES
1. To trigger the rethumbing of "pydiods1" datasource:
$ ` + os.Args[0] + ` admin datasource rethumb --datasource=pydiods1
2. Process only the folder/subfolder data :
$ ` + os.Args[0] + ` admin datasource rethumb --datasource=pydiods1 --path=folder/subfolder
`,
Run: func(cmd *cobra.Command, args []string) {
if rethumbDsName == "" || rethumbUserName == "" {
cmd.Println("Please provide at least a datasource name (--datasource) and an admin user name")
cmd.Help()
return
}
ap, _ := anypb.New(&tree.Query{
MinSize: 1,
Type: tree.NodeType_LEAF,
PathPrefix: []string{rethumbDsName},
FreeString: "-Meta.is_image:T* Extension:jpg Extension:jpeg Extension:png Extension:bmp",
})
jobClient := jobs.NewJobServiceClient(grpc.GetClientConnFromCtx(ctx, common.ServiceJobs))
job := &jobs.Job{
ID: uuid.New(),
Owner: rethumbUserName,
Label: "Find and recompute missing thumbnails in " + rethumbDsName,
AutoStart: true,
AutoClean: true,
MaxConcurrency: int32(rethumbMaxConcurrency),
Actions: []*jobs.Action{
{
ID: "actions.images.thumbnails",
Parameters: map[string]string{},
NodesSelector: &jobs.NodesSelector{
Query: &service.Query{
SubQueries: []*anypb.Any{ap},
Operation: service.OperationType_AND,
},
Label: "Images w/o 'is_image' metadata",
},
},
},
Timeout: rethumbTimeout,
}
if _, err := jobClient.PutJob(ctx, &jobs.PutJobRequest{Job: job}); err != nil {
cmd.Println(promptui.IconBad + " [ERROR] " + err.Error())
} else {
cmd.Println(promptui.IconGood + " [SUCCESS] Posted job for recomputing hashes on all files")
}
},
}
func init() {
dsRethumbCmd.PersistentFlags().StringVarP(&rethumbDsName, "datasource", "d", "pydiods1", "Name of datasource to process")
dsRethumbCmd.PersistentFlags().StringVarP(&rethumbUserName, "username", "u", "", "Username under which the job will be executed (generally admin)")
dsRethumbCmd.PersistentFlags().StringVarP(&rethumbTimeout, "timeout", "t", "30m", "Maximum job duration")
dsRethumbCmd.PersistentFlags().IntVarP(&rethumbMaxConcurrency, "concurrency", "c", 10, "Maximum concurrency for computing files hashes")
DataSourceCmd.AddCommand(dsRethumbCmd)
}