zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
 *
 * (c) Copyright Ascensio System SIA 2021
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
 
package org.jeecg.modules.doc.entity;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
 
import lombok.Data;
import org.jeecg.modules.doc.util.DocumentManager;
import org.jeecg.modules.doc.util.FileUtility;
 
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
 
@Data
public class FileModel
{
    public String type = "desktop";
    public String mode = "edit";
    public String documentType;
    public Document document;
    public EditorConfig editorConfig;
    public String token;
 
    public FileModel(String userFileId, String fileName, String fileUrl, String fileModifyTime, String uid, String uname, String callbackUrl, String mode)
    {
        if (fileName == null) fileName = "";
        fileName = fileName.trim();
 
        documentType = FileUtility.GetFileType(fileName).toString().toLowerCase();
 
        document = new Document();
        document.title = fileName;
        document.url = fileUrl;
//        document.urlUser = DocumentManager.GetFileUri(fileName, false);
        document.fileType = FileUtility.GetFileExtension(fileName).replace(".", "");
        document.key = Base64.getEncoder().encodeToString((userFileId + "_" + fileModifyTime).getBytes(StandardCharsets.UTF_8));
        document.info = new Info();
        document.info.favorite = uid != null && !uid.isEmpty() ? uid.equals("uid-2") : null;
 
        editorConfig = new EditorConfig(null);
        editorConfig.callbackUrl = callbackUrl;
//        editorConfig.lang = null;
 
        if (uid != null) editorConfig.user.id = uid;
        if (uname != null) editorConfig.user.name = uid.equals("uid-0") ? null : uname;
        if (editorConfig.user.id.equals("uid-2")) editorConfig.user.group = "group-2";
        if (editorConfig.user.id.equals("uid-3")) editorConfig.user.group = "group-3";
 
//        editorConfig.customization.goback.url = DocumentManager.GetServerUrl(false) + "/IndexServlet";
 
        changeType(mode, type);
    }
 
    public void changeType(String _mode, String _type)
    {
        if (_mode != null) mode = _mode;
        if (_type != null) type = _type;
 
        Boolean canEdit = DocumentManager.GetEditedExts().contains(FileUtility.GetFileExtension(document.title));
        editorConfig.customization.submitForm = canEdit && (mode.equals("edit") || mode.equals("fillForms"));
        editorConfig.mode = canEdit && !mode.equals("view") ? "edit" : "view";
 
        document.permissions = new Permissions(mode, type, canEdit);
 
        if (type.equals("embedded")) InitDesktop();
    }
 
    public void InitDesktop()
    {
        editorConfig.InitDesktop(document.urlUser);
    }
 
 
    private String readFileToEnd(File file) {
        String output = "";
        try {
            try(FileInputStream is = new FileInputStream(file))
            {
                Scanner scanner = new Scanner(is);
                scanner.useDelimiter("\\A");
                while (scanner.hasNext()) {
                    output += scanner.next();
                }
                scanner.close();
            }
        } catch (Exception e) { }
        return output;
    }
 
    @Data
    public class Document
    {
        public String title;
        public String url;
        public String urlUser;
        public String fileType;
        public String key;
        public Info info;
        public Permissions permissions;
    }
 
    @Data
    public class Permissions
    {
        public Boolean comment;
        public Boolean download;
        public Boolean edit;
        public Boolean fillForms;
        public Boolean modifyFilter;
        public Boolean modifyContentControl;
        public Boolean review;
        public List<String> reviewGroups;
 
        public Permissions(String mode, String type, Boolean canEdit)
        {
            comment = !mode.equals("view") && !mode.equals("fillForms") && !mode.equals("embedded") && !mode.equals("blockcontent");
            download = false;
            edit = canEdit && (mode.equals("edit") || mode.equals("view") || mode.equals("filter") || mode.equals("blockcontent"));
            fillForms = !mode.equals("view") && !mode.equals("comment") && !mode.equals("embedded") && !mode.equals("blockcontent");
            modifyFilter = !mode.equals("filter");
            modifyContentControl = !mode.equals("blockcontent");
            review = mode.equals("edit") || mode.equals("review");
            reviewGroups = editorConfig.user.group != null ? GetReviewGroups(editorConfig.user.group) : null;
        }
 
        private List<String> GetReviewGroups(String group){
            Map<String, List<String>> reviewGroups = new HashMap<>();
 
            reviewGroups.put("group-2", Arrays.asList("group-2", ""));
            reviewGroups.put("group-3", Arrays.asList("group-2"));
 
            return reviewGroups.get(group);
        }
    }
 
    @Data
    public class Info
    {
        Boolean favorite;
    }
 
    public class EditorConfig
    {
        public HashMap<String, Object> actionLink = null;
        public String mode = "edit";
        public String callbackUrl;
        public String lang = "en";
        public User user;
        public Customization customization;
        public Embedded embedded;
 
        public EditorConfig(String actionData)
        {
            if (actionData != null) {
                Gson gson = new Gson();
                actionLink = gson.fromJson(actionData, new TypeToken<HashMap<String, Object>>() { }.getType());
            }
            user = new User();
            customization = new Customization();
        }
 
        public void InitDesktop(String url)
        {
            embedded = new Embedded();
            embedded.saveUrl = url;
            embedded.embedUrl = url;
            embedded.shareUrl = url;
            embedded.toolbarDocked = "top";
        }
 
        public class User
        {
            public String id = "uid-1";
            public String name = "John Smith";
            public String group = null;
        }
 
        public class Customization
        {
            public Goback goback;
            public Boolean forcesave;
            public Boolean submitForm;
 
            public Customization()
            {
                forcesave = false;
                goback = new Goback();
            }
 
            public class Goback
            {
                public String url;
            }
        }
 
        public class Embedded
        {
            public String saveUrl;
            public String embedUrl;
            public String shareUrl;
            public String toolbarDocked;
        }
    }
 
 
    public static String Serialize(FileModel model)
    {
        Gson gson = new Gson();
        return gson.toJson(model);
    }
}