diff --git a/web/documentserver-example/csharp-mvc/Scripts/formats.js b/web/documentserver-example/csharp-mvc/Scripts/formats.js new file mode 100644 index 00000000..e423eeb2 --- /dev/null +++ b/web/documentserver-example/csharp-mvc/Scripts/formats.js @@ -0,0 +1,66 @@ +/** + * + * (c) Copyright Ascensio System SIA 2024 + * + * 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. + * + */ + +class Format { + constructor(name, type, actions, convert, mime) { + this.name = name; + this.type = type; + this.actions = actions; + this.convert = convert; + this.mime = mime; + } + + isAutoConvertible() { + return this.actions.includes('auto-convert'); + } + + isEditable() { + return this.actions.includes('edit') || this.actions.includes('lossy-edit'); + } + + isFillable() { + return this.actions.includes('fill'); + } +} + +class FormatManager { + formats = []; + + constructor(formats) { + if(Array.isArray(formats)) this.formats = formats; + } + + findByExtension(extension) { + return this.formats.find(format => format.name == extension); + } + + isAutoConvertible(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isAutoConvertible(); + } + + isEditable(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isEditable(); + } + + isFillable(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isFillable(); + } +} \ No newline at end of file diff --git a/web/documentserver-example/csharp-mvc/Scripts/jscript.js b/web/documentserver-example/csharp-mvc/Scripts/jscript.js index 4399c79a..d74be36c 100644 --- a/web/documentserver-example/csharp-mvc/Scripts/jscript.js +++ b/web/documentserver-example/csharp-mvc/Scripts/jscript.js @@ -17,6 +17,27 @@ */ var directUrl; +var formatManager; + +window.onload = function () { + fetch("webeditor.ashx?type=formats") + .then((response) => response.json()) + .then((data) => { + if (data.formats) { + let formats = []; + data.formats.forEach(format => { + formats.push(new Format( + format.Name, + format.Type, + format.Actions, + format.Convert, + format.Mime + )); + }); + formatManager = new FormatManager(formats); + } + }) +} if (typeof jQuery != "undefined") { jq = jQuery.noConflict(); @@ -103,7 +124,7 @@ if (typeof jQuery != "undefined") { var posExt = fileName.lastIndexOf('.'); posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ''; - if (ConverExtList.indexOf(posExt) == -1) { + if (!formatManager.isAutoConvertible(posExt)) { jq("#step2").addClass("done").removeClass("current"); loadScripts(); return; @@ -184,10 +205,10 @@ if (typeof jQuery != "undefined") { jq("#beginView, #beginEmbedded").removeClass("disable"); var fileName = jq("#hiddenFileName").val(); - var posExt = fileName.lastIndexOf('.'); + var posExt = fileName.lastIndexOf('.') + 1; posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ''; - if (EditedExtList.indexOf(posExt) != -1 || FillExtList.indexOf(posExt) != -1) { + if (formatManager.isEditable(posExt) || formatManager.isFillable(posExt)) { jq("#beginEdit").removeClass("disable"); } }; diff --git a/web/documentserver-example/csharp-mvc/Views/Home/Index.aspx b/web/documentserver-example/csharp-mvc/Views/Home/Index.aspx index 639ab4c9..fdf0ec31 100644 --- a/web/documentserver-example/csharp-mvc/Views/Home/Index.aspx +++ b/web/documentserver-example/csharp-mvc/Views/Home/Index.aspx @@ -373,9 +373,6 @@ <%: Scripts.Render("~/bundles/jquery", "~/bundles/scripts") %> diff --git a/web/documentserver-example/csharp-mvc/WebEditor.ashx.cs b/web/documentserver-example/csharp-mvc/WebEditor.ashx.cs index 8eaa1897..f3c0bb01 100644 --- a/web/documentserver-example/csharp-mvc/WebEditor.ashx.cs +++ b/web/documentserver-example/csharp-mvc/WebEditor.ashx.cs @@ -87,6 +87,9 @@ namespace OnlineEditorsExampleMVC case "reference": Reference(context); break; + case "formats": + Formats(context); + break; } } @@ -974,6 +977,25 @@ namespace OnlineEditorsExampleMVC return history; } + // return all the supported formats + private static void Formats(HttpContext context) + { + try + { + Dictionary data = new Dictionary + { + { "formats", FormatManager.All() } + }; + context.Response.ContentType = "application/json"; + var jss = new JavaScriptSerializer(); + + context.Response.Write(jss.Serialize(data)); + } + catch (Exception e) + { + context.Response.Write("{ \"error\": \"" + e.Message + "\"}"); + } + } } } \ No newline at end of file diff --git a/web/documentserver-example/csharp/Default.aspx b/web/documentserver-example/csharp/Default.aspx index 4f207cfb..a0f027e2 100644 --- a/web/documentserver-example/csharp/Default.aspx +++ b/web/documentserver-example/csharp/Default.aspx @@ -380,12 +380,8 @@ + - diff --git a/web/documentserver-example/csharp/WebEditor.ashx.cs b/web/documentserver-example/csharp/WebEditor.ashx.cs index 89880424..f567af51 100644 --- a/web/documentserver-example/csharp/WebEditor.ashx.cs +++ b/web/documentserver-example/csharp/WebEditor.ashx.cs @@ -87,6 +87,9 @@ namespace OnlineEditorsExample case "reference": Reference(context); break; + case "formats": + Formats(context); + break; } } @@ -788,5 +791,25 @@ namespace OnlineEditorsExample + userAddress; return fileUrl.ToString(); } + + // return all the supported formats + private static void Formats(HttpContext context) + { + try + { + Dictionary data = new Dictionary + { + { "formats", FormatManager.All() } + }; + context.Response.ContentType = "application/json"; + var jss = new JavaScriptSerializer(); + + context.Response.Write(jss.Serialize(data)); + } + catch (Exception e) + { + context.Response.Write("{ \"error\": \"" + e.Message + "\"}"); + } + } } } \ No newline at end of file diff --git a/web/documentserver-example/csharp/script/formats.js b/web/documentserver-example/csharp/script/formats.js new file mode 100644 index 00000000..e423eeb2 --- /dev/null +++ b/web/documentserver-example/csharp/script/formats.js @@ -0,0 +1,66 @@ +/** + * + * (c) Copyright Ascensio System SIA 2024 + * + * 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. + * + */ + +class Format { + constructor(name, type, actions, convert, mime) { + this.name = name; + this.type = type; + this.actions = actions; + this.convert = convert; + this.mime = mime; + } + + isAutoConvertible() { + return this.actions.includes('auto-convert'); + } + + isEditable() { + return this.actions.includes('edit') || this.actions.includes('lossy-edit'); + } + + isFillable() { + return this.actions.includes('fill'); + } +} + +class FormatManager { + formats = []; + + constructor(formats) { + if(Array.isArray(formats)) this.formats = formats; + } + + findByExtension(extension) { + return this.formats.find(format => format.name == extension); + } + + isAutoConvertible(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isAutoConvertible(); + } + + isEditable(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isEditable(); + } + + isFillable(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isFillable(); + } +} \ No newline at end of file diff --git a/web/documentserver-example/csharp/script/jscript.js b/web/documentserver-example/csharp/script/jscript.js index abdcc780..9a8cd0ec 100644 --- a/web/documentserver-example/csharp/script/jscript.js +++ b/web/documentserver-example/csharp/script/jscript.js @@ -17,6 +17,27 @@ */ var directUrl; +var formatManager; + +window.onload = function () { + fetch("webeditor.ashx?type=formats") + .then((response) => response.json()) + .then((data) => { + if (data.formats) { + let formats = []; + data.formats.forEach(format => { + formats.push(new Format( + format.Name, + format.Type, + format.Actions, + format.Convert, + format.Mime + )); + }); + formatManager = new FormatManager(formats); + } + }) +} if (typeof jQuery != "undefined") { jq = jQuery.noConflict(); @@ -103,7 +124,7 @@ if (typeof jQuery != "undefined") { var posExt = fileName.lastIndexOf('.'); posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ''; - if (ConverExtList.indexOf(posExt) == -1) { + if (!formatManager.isAutoConvertible(posExt)) { jq("#step2").addClass("done").removeClass("current"); loadScripts(); return; @@ -184,10 +205,10 @@ if (typeof jQuery != "undefined") { jq("#beginView, #beginEmbedded").removeClass("disable"); var fileName = jq("#hiddenFileName").val(); - var posExt = fileName.lastIndexOf('.'); + var posExt = fileName.lastIndexOf('.') + 1; posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ''; - if (EditedExtList.indexOf(posExt) != -1 || FillFormExtList.indexOf(posExt) != -1) { + if (formatManager.isEditable(posExt) || formatManager.isFillable(posExt)) { jq("#beginEdit").removeClass("disable"); } }; diff --git a/web/documentserver-example/java-spring/src/main/java/com/onlyoffice/integration/controllers/IndexController.java b/web/documentserver-example/java-spring/src/main/java/com/onlyoffice/integration/controllers/IndexController.java index 6de1d9d2..dba6bfc1 100755 --- a/web/documentserver-example/java-spring/src/main/java/com/onlyoffice/integration/controllers/IndexController.java +++ b/web/documentserver-example/java-spring/src/main/java/com/onlyoffice/integration/controllers/IndexController.java @@ -22,8 +22,10 @@ import com.onlyoffice.integration.documentserver.storage.FileStorageMutator; import com.onlyoffice.integration.documentserver.storage.FileStoragePathBuilder; import com.onlyoffice.integration.documentserver.util.Misc; import com.onlyoffice.integration.documentserver.util.file.FileUtility; +import com.onlyoffice.integration.documentserver.util.service.FormatService; import com.onlyoffice.integration.entities.User; import com.onlyoffice.integration.services.UserServices; +import com.onlyoffice.integration.dto.FormatsList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; @@ -33,6 +35,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.http.ResponseEntity; import java.util.ArrayList; import java.util.Arrays; @@ -61,6 +64,9 @@ public class IndexController { @Autowired private UserServices userService; + @Autowired + private FormatService formatService; + @Value("${files.docservice.url.site}") private String docserviceSite; @@ -136,16 +142,16 @@ public class IndexController { @ResponseBody public HashMap configParameters() { // get configuration parameters HashMap configuration = new HashMap<>(); - - configuration.put("FillExtList", String.join(",", fileUtility - .getFillExts())); // put a list of the extensions that can be filled to config - configuration.put("ConverExtList", String.join(",", fileUtility - .getConvertExts())); // put a list of the extensions that can be converted to config - configuration.put("EditedExtList", String.join(",", fileUtility - .getEditedExts())); // put a list of the extensions that can be edited to config configuration.put("UrlConverter", urlConverter); configuration.put("UrlEditor", urlEditor); return configuration; } + + @GetMapping("/formats") + @ResponseBody + public ResponseEntity formats() { // return all the supported formats + FormatsList list = new FormatsList(formatService.getFormats()); + return ResponseEntity.ok(list); + } } diff --git a/web/documentserver-example/java-spring/src/main/java/com/onlyoffice/integration/dto/FormatsList.java b/web/documentserver-example/java-spring/src/main/java/com/onlyoffice/integration/dto/FormatsList.java new file mode 100644 index 00000000..daa58983 --- /dev/null +++ b/web/documentserver-example/java-spring/src/main/java/com/onlyoffice/integration/dto/FormatsList.java @@ -0,0 +1,32 @@ +/** + * + * (c) Copyright Ascensio System SIA 2024 + * + * 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 com.onlyoffice.integration.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import com.onlyoffice.integration.documentserver.models.Format; +import java.util.List; + +@Getter +@Setter +@AllArgsConstructor +public class FormatsList { + private List formats; +} diff --git a/web/documentserver-example/java-spring/src/main/resources/static/scripts/converter.js b/web/documentserver-example/java-spring/src/main/resources/static/scripts/converter.js index 6a804a0d..47f86373 100755 --- a/web/documentserver-example/java-spring/src/main/resources/static/scripts/converter.js +++ b/web/documentserver-example/java-spring/src/main/resources/static/scripts/converter.js @@ -16,18 +16,12 @@ * */ -var ConverExtList; -var EditedExtList; var UrlConverter; var UrlEditor; -var FillExtList; if (typeof jQuery !== "undefined") { jQuery.post('/config', function(data) { - FillExtList = data.FillExtList.split(','); - ConverExtList = data.ConverExtList.split(','); - EditedExtList = data.EditedExtList.split(','); UrlConverter = data.UrlConverter; UrlEditor = data.UrlEditor; }); diff --git a/web/documentserver-example/java-spring/src/main/resources/static/scripts/formats.js b/web/documentserver-example/java-spring/src/main/resources/static/scripts/formats.js new file mode 100644 index 00000000..e423eeb2 --- /dev/null +++ b/web/documentserver-example/java-spring/src/main/resources/static/scripts/formats.js @@ -0,0 +1,66 @@ +/** + * + * (c) Copyright Ascensio System SIA 2024 + * + * 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. + * + */ + +class Format { + constructor(name, type, actions, convert, mime) { + this.name = name; + this.type = type; + this.actions = actions; + this.convert = convert; + this.mime = mime; + } + + isAutoConvertible() { + return this.actions.includes('auto-convert'); + } + + isEditable() { + return this.actions.includes('edit') || this.actions.includes('lossy-edit'); + } + + isFillable() { + return this.actions.includes('fill'); + } +} + +class FormatManager { + formats = []; + + constructor(formats) { + if(Array.isArray(formats)) this.formats = formats; + } + + findByExtension(extension) { + return this.formats.find(format => format.name == extension); + } + + isAutoConvertible(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isAutoConvertible(); + } + + isEditable(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isEditable(); + } + + isFillable(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isFillable(); + } +} \ No newline at end of file diff --git a/web/documentserver-example/java-spring/src/main/resources/static/scripts/jscript.js b/web/documentserver-example/java-spring/src/main/resources/static/scripts/jscript.js index 443b4e85..d95a8f30 100755 --- a/web/documentserver-example/java-spring/src/main/resources/static/scripts/jscript.js +++ b/web/documentserver-example/java-spring/src/main/resources/static/scripts/jscript.js @@ -17,6 +17,27 @@ */ var directUrl; +var formatManager; + +window.onload = function () { + fetch('/formats') + .then((response) => response.json()) + .then((data) => { + if (data.formats) { + let formats = []; + data.formats.forEach(format => { + formats.push(new Format( + format.name, + format.type, + format.actions, + format.convert, + format.mime + )); + }); + formatManager = new FormatManager(formats); + } + }) +} if (typeof jQuery !== "undefined") { jq = jQuery.noConflict(); @@ -103,7 +124,7 @@ if (typeof jQuery !== "undefined") { var posExt = fileName.lastIndexOf(".") + 1; posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ""; - if (ConverExtList.includes(posExt) === -1) { + if (!formatManager.isAutoConvertible(posExt)) { jq("#step2").addClass("done").removeClass("current"); loadScripts(); return; @@ -186,7 +207,7 @@ if (typeof jQuery !== "undefined") { var posExt = fileName.lastIndexOf(".") + 1; posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ""; - if (EditedExtList.includes(posExt) !== -1 || FillExtList.includes(posExt) !== -1) { + if (formatManager.isEditable(posExt) || formatManager.isFillable(posExt)) { jq("#beginEdit").removeClass("disable"); } }; diff --git a/web/documentserver-example/java-spring/src/main/resources/templates/index.html b/web/documentserver-example/java-spring/src/main/resources/templates/index.html index ad908ae5..98c51cfa 100755 --- a/web/documentserver-example/java-spring/src/main/resources/templates/index.html +++ b/web/documentserver-example/java-spring/src/main/resources/templates/index.html @@ -357,6 +357,7 @@ + + + diff --git a/web/documentserver-example/nodejs/views/wopiIndex.ejs b/web/documentserver-example/nodejs/views/wopiIndex.ejs index f090d579..2e2dce20 100755 --- a/web/documentserver-example/nodejs/views/wopiIndex.ejs +++ b/web/documentserver-example/nodejs/views/wopiIndex.ejs @@ -274,12 +274,10 @@ + diff --git a/web/documentserver-example/php/assets/js/formats.js b/web/documentserver-example/php/assets/js/formats.js new file mode 100644 index 00000000..e423eeb2 --- /dev/null +++ b/web/documentserver-example/php/assets/js/formats.js @@ -0,0 +1,66 @@ +/** + * + * (c) Copyright Ascensio System SIA 2024 + * + * 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. + * + */ + +class Format { + constructor(name, type, actions, convert, mime) { + this.name = name; + this.type = type; + this.actions = actions; + this.convert = convert; + this.mime = mime; + } + + isAutoConvertible() { + return this.actions.includes('auto-convert'); + } + + isEditable() { + return this.actions.includes('edit') || this.actions.includes('lossy-edit'); + } + + isFillable() { + return this.actions.includes('fill'); + } +} + +class FormatManager { + formats = []; + + constructor(formats) { + if(Array.isArray(formats)) this.formats = formats; + } + + findByExtension(extension) { + return this.formats.find(format => format.name == extension); + } + + isAutoConvertible(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isAutoConvertible(); + } + + isEditable(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isEditable(); + } + + isFillable(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isFillable(); + } +} \ No newline at end of file diff --git a/web/documentserver-example/php/assets/js/jscript.js b/web/documentserver-example/php/assets/js/jscript.js index dd415e08..3ab02c28 100644 --- a/web/documentserver-example/php/assets/js/jscript.js +++ b/web/documentserver-example/php/assets/js/jscript.js @@ -17,6 +17,27 @@ */ var directUrl; +var formatManager; + +window.onload = function () { + fetch('formats') + .then((response) => response.json()) + .then((data) => { + if (data.formats) { + let formats = []; + JSON.parse(data.formats).forEach(format => { + formats.push(new Format( + format.name, + format.type, + format.actions, + format.convert, + format.mime + )); + }); + formatManager = new FormatManager(formats); + } + }) +} if (typeof jQuery != "undefined") { jq = jQuery.noConflict(); @@ -111,7 +132,7 @@ if (typeof jQuery != "undefined") { var posExt = fileName.lastIndexOf('.') + 1; posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ''; - if (ConverExtList.indexOf(posExt) == -1) { + if (!formatManager.isAutoConvertible(posExt)) { jq("#step2").addClass("done").removeClass("current"); loadScripts(); return; @@ -206,9 +227,9 @@ if (typeof jQuery != "undefined") { var fileName = jq("#hiddenFileName").val(); var posExt = fileName.lastIndexOf('.') + 1; - posExt = 0 <= posExt ? fileName.substring(posExt + 1).trim().toLowerCase() : ''; + posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ''; - if (EditedExtList.indexOf(posExt) != -1 || FillFormsExtList.indexOf(posExt) != -1) { + if (formatManager.isEditable(posExt) || formatManager.isFillable(posExt)) { jq("#beginEdit").removeClass("disable"); } }; diff --git a/web/documentserver-example/php/index.php b/web/documentserver-example/php/index.php index fa17b217..04b5be69 100755 --- a/web/documentserver-example/php/index.php +++ b/web/documentserver-example/php/index.php @@ -143,6 +143,11 @@ function routers() echo json_encode($response); return; } + if (str_starts_with($path, '/formats')) { + $response = formats(); + echo json_encode($response); + return; + } http_response_code(HTTPStatus::NotFound->value); } diff --git a/web/documentserver-example/php/src/ajax.php b/web/documentserver-example/php/src/ajax.php index ef343ada..f6cc2ebe 100644 --- a/web/documentserver-example/php/src/ajax.php +++ b/web/documentserver-example/php/src/ajax.php @@ -659,3 +659,19 @@ function restore() ]; } } + +function formats() +{ + try { + $formatManager = new FormatManager(); + $formats = $formatManager->all(); + + return [ + 'formats' => json_encode($formats) + ]; + } catch (Exception $error) { + return [ + 'error' => 'Server error' + ]; + } +} diff --git a/web/documentserver-example/php/src/views/IndexView.php b/web/documentserver-example/php/src/views/IndexView.php index 604bb646..e368906c 100644 --- a/web/documentserver-example/php/src/views/IndexView.php +++ b/web/documentserver-example/php/src/views/IndexView.php @@ -44,9 +44,6 @@ final class IndexView extends View "editButton" => $this->getEditButton(), "dataDocs" => $this->getPreloaderUrl(), "date" => date("Y"), - "fillFormsExtList" => implode(",", $formatManager->fillableExtensions()), - "converExtList" => implode(",", $formatManager->convertibleExtensions()), - "editedExtList" => implode(",", $formatManager->editableExtensions()), "serverVersion" => $configManager -> getVersion(), ]; } diff --git a/web/documentserver-example/php/templates/index.tpl b/web/documentserver-example/php/templates/index.tpl index fa221f4d..165ed059 100644 --- a/web/documentserver-example/php/templates/index.tpl +++ b/web/documentserver-example/php/templates/index.tpl @@ -220,11 +220,7 @@ + - \ No newline at end of file diff --git a/web/documentserver-example/python/manage.py b/web/documentserver-example/python/manage.py index d861336e..26d762bf 100644 --- a/web/documentserver-example/python/manage.py +++ b/web/documentserver-example/python/manage.py @@ -80,7 +80,8 @@ def routers(): path('restore', actions.restore), path('saveas', actions.saveAs), path('track', actions.track), - path('upload', actions.upload) + path('upload', actions.upload), + path('formats', actions.formats) ] main += static( settings.STATIC_URL, diff --git a/web/documentserver-example/python/src/views/actions.py b/web/documentserver-example/python/src/views/actions.py index 89d41e6e..abc2c260 100755 --- a/web/documentserver-example/python/src/views/actions.py +++ b/web/documentserver-example/python/src/views/actions.py @@ -30,6 +30,8 @@ from src.configuration import ConfigurationManager from src.response import ErrorResponse from src.utils import docManager, fileUtils, serviceConverter, users, jwtManager, historyManager, trackManager from urllib.parse import urlparse, parse_qs +from src.format import FormatManager +import msgspec config_manager = ConfigurationManager() @@ -667,3 +669,12 @@ def restore(request: HttpRequest) -> HttpResponse: message=f'{type(error)}: {error}', status=HTTPStatus.INTERNAL_SERVER_ERROR ) + + +@http.GET() +def formats(request: HttpRequest) -> HttpResponse: + data = { + 'formats': [msgspec.to_builtins(format) for format in FormatManager().all()] + } + + return HttpResponse(json.dumps(data), content_type='application/json') diff --git a/web/documentserver-example/python/src/views/index.py b/web/documentserver-example/python/src/views/index.py index b15cfad6..4f1b87fd 100755 --- a/web/documentserver-example/python/src/views/index.py +++ b/web/documentserver-example/python/src/views/index.py @@ -16,17 +16,13 @@ """ -import json - from django.shortcuts import render from src.configuration import ConfigurationManager -from src.format import FormatManager from src.utils import users from src.utils import docManager config_manager = ConfigurationManager() -format_manager = FormatManager() def getDirectUrlParam(request): @@ -41,10 +37,7 @@ def default(request): # default parameters that will be passed to the template 'users': users.USERS, 'languages': config_manager.languages(), 'preloadurl': config_manager.document_server_preloader_url().geturl(), - 'editExt': json.dumps(format_manager.editable_extensions()), # file extensions that can be edited - 'convExt': json.dumps(format_manager.convertible_extensions()), # file extensions that can be converted 'files': docManager.getStoredFiles(request), # information about stored files - 'fillExt': json.dumps(format_manager.fillable_extensions()), 'directUrl': str(getDirectUrlParam(request)).lower, 'serverVersion': config_manager.getVersion() } diff --git a/web/documentserver-example/python/static/js/formats.js b/web/documentserver-example/python/static/js/formats.js new file mode 100644 index 00000000..e423eeb2 --- /dev/null +++ b/web/documentserver-example/python/static/js/formats.js @@ -0,0 +1,66 @@ +/** + * + * (c) Copyright Ascensio System SIA 2024 + * + * 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. + * + */ + +class Format { + constructor(name, type, actions, convert, mime) { + this.name = name; + this.type = type; + this.actions = actions; + this.convert = convert; + this.mime = mime; + } + + isAutoConvertible() { + return this.actions.includes('auto-convert'); + } + + isEditable() { + return this.actions.includes('edit') || this.actions.includes('lossy-edit'); + } + + isFillable() { + return this.actions.includes('fill'); + } +} + +class FormatManager { + formats = []; + + constructor(formats) { + if(Array.isArray(formats)) this.formats = formats; + } + + findByExtension(extension) { + return this.formats.find(format => format.name == extension); + } + + isAutoConvertible(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isAutoConvertible(); + } + + isEditable(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isEditable(); + } + + isFillable(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isFillable(); + } +} \ No newline at end of file diff --git a/web/documentserver-example/python/static/js/jscript.js b/web/documentserver-example/python/static/js/jscript.js index 7a4d1acc..10c899b3 100644 --- a/web/documentserver-example/python/static/js/jscript.js +++ b/web/documentserver-example/python/static/js/jscript.js @@ -17,6 +17,27 @@ */ var directUrl; +var formatManager; + +window.onload = function () { + fetch('formats') + .then((response) => response.json()) + .then((data) => { + if (data.formats) { + let formats = []; + data.formats.forEach(format => { + formats.push(new Format( + format.name, + format.type, + format.actions, + format.convert, + format.mime + )); + }); + formatManager = new FormatManager(formats); + } + }) +} if (typeof jQuery !== "undefined") { jq = jQuery.noConflict(); @@ -103,7 +124,7 @@ if (typeof jQuery !== "undefined") { var posExt = fileName.lastIndexOf("."); posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ""; - if (ConverExtList.indexOf(posExt) === -1) { + if (!formatManager.isAutoConvertible(posExt)) { jq("#step2").addClass("done").removeClass("current"); loadScripts(); return; @@ -183,10 +204,10 @@ if (typeof jQuery !== "undefined") { jq("#beginView, #beginEmbedded").removeClass("disable"); var fileName = jq("#hiddenFileName").val(); - var posExt = fileName.lastIndexOf("."); + var posExt = fileName.lastIndexOf(".") + 1; posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ""; - if (EditedExtList.indexOf(posExt) !== -1 || FillExtList.indexOf(posExt) !== -1) { + if (formatManager.isEditable(posExt) || formatManager.isFillable(posExt)) { jq("#beginEdit").removeClass("disable"); } }; diff --git a/web/documentserver-example/python/templates/index.html b/web/documentserver-example/python/templates/index.html index 1fcd008d..26240469 100644 --- a/web/documentserver-example/python/templates/index.html +++ b/web/documentserver-example/python/templates/index.html @@ -354,12 +354,10 @@ + diff --git a/web/documentserver-example/ruby/app/assets/javascripts/formats.js b/web/documentserver-example/ruby/app/assets/javascripts/formats.js new file mode 100644 index 00000000..e423eeb2 --- /dev/null +++ b/web/documentserver-example/ruby/app/assets/javascripts/formats.js @@ -0,0 +1,66 @@ +/** + * + * (c) Copyright Ascensio System SIA 2024 + * + * 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. + * + */ + +class Format { + constructor(name, type, actions, convert, mime) { + this.name = name; + this.type = type; + this.actions = actions; + this.convert = convert; + this.mime = mime; + } + + isAutoConvertible() { + return this.actions.includes('auto-convert'); + } + + isEditable() { + return this.actions.includes('edit') || this.actions.includes('lossy-edit'); + } + + isFillable() { + return this.actions.includes('fill'); + } +} + +class FormatManager { + formats = []; + + constructor(formats) { + if(Array.isArray(formats)) this.formats = formats; + } + + findByExtension(extension) { + return this.formats.find(format => format.name == extension); + } + + isAutoConvertible(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isAutoConvertible(); + } + + isEditable(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isEditable(); + } + + isFillable(extension) { + let format = this.findByExtension(extension); + return format !== undefined && format.isFillable(); + } +} \ No newline at end of file diff --git a/web/documentserver-example/ruby/app/assets/javascripts/jscript.js b/web/documentserver-example/ruby/app/assets/javascripts/jscript.js index 2972054a..41e5ff3a 100644 --- a/web/documentserver-example/ruby/app/assets/javascripts/jscript.js +++ b/web/documentserver-example/ruby/app/assets/javascripts/jscript.js @@ -18,6 +18,27 @@ var directUrl; var userId; +var formatManager; + +window.onload = function () { + fetch('formats') + .then((response) => response.json()) + .then((data) => { + if (data.formats) { + let formats = []; + data.formats.forEach(format => { + formats.push(new Format( + format.name, + format.type, + format.actions, + format.convert, + format.mime + )); + }); + formatManager = new FormatManager(formats); + } + }) +} if (typeof jQuery != "undefined") { jq = jQuery.noConflict(); @@ -111,10 +132,10 @@ if (typeof jQuery != "undefined") { jq("#filePass").val(""); var fileName = jq("#hiddenFileName").val(); - var posExt = fileName.lastIndexOf('.'); + var posExt = fileName.lastIndexOf('.') + 1; posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ''; - if (ConvertExtList.indexOf(posExt) == -1) { + if (!formatManager.isAutoConvertible(posExt)) { jq("#step2").addClass("done").removeClass("current"); loadScripts(); return; @@ -194,10 +215,10 @@ if (typeof jQuery != "undefined") { jq("#beginView, #beginEmbedded").removeClass("disable"); var fileName = jq("#hiddenFileName").val(); - var posExt = fileName.lastIndexOf('.'); + var posExt = fileName.lastIndexOf('.') + 1; posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : ''; - if (EditedExtList.indexOf(posExt) != -1 || FillExtList.indexOf(posExt) != -1) { + if (formatManager.isEditable(posExt) || formatManager.isFillable(posExt)) { jq("#beginEdit").removeClass("disable"); } }; diff --git a/web/documentserver-example/ruby/app/controllers/home_controller.rb b/web/documentserver-example/ruby/app/controllers/home_controller.rb index 9d454425..01df590e 100755 --- a/web/documentserver-example/ruby/app/controllers/home_controller.rb +++ b/web/documentserver-example/ruby/app/controllers/home_controller.rb @@ -547,4 +547,15 @@ class HomeController < ApplicationController } ) end + + # return all supported formats + def formats + render( + json: JSON.generate( + { + formats: FormatManager.new.all.map(&:serialize) + } + ) + ) + end end diff --git a/web/documentserver-example/ruby/app/views/home/index.html.erb b/web/documentserver-example/ruby/app/views/home/index.html.erb index 96600c39..94d9132b 100755 --- a/web/documentserver-example/ruby/app/views/home/index.html.erb +++ b/web/documentserver-example/ruby/app/views/home/index.html.erb @@ -345,9 +345,6 @@ <%= javascript_include_tag "application" %> diff --git a/web/documentserver-example/ruby/config/application.rb b/web/documentserver-example/ruby/config/application.rb index 82525745..6442a7c8 100644 --- a/web/documentserver-example/ruby/config/application.rb +++ b/web/documentserver-example/ruby/config/application.rb @@ -46,5 +46,6 @@ class Application < Rails::Application match '/saveas', to: 'home#saveas', via: 'post' match '/track', to: 'home#track', via: 'post' match '/upload', to: 'home#upload', via: 'post' + match '/formats', to: 'home#formats', via: 'get' end end