/** * * (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. * */ using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using static OnlineEditorsExampleMVC.Models.FileUtility; using System.Linq; using System.Text; using Newtonsoft.Json.Converters; namespace OnlineEditorsExampleMVC.Models { public static class FileUtility { public enum FileType { Word, Cell, Slide, Pdf } // get file type public static FileType GetFileType(string fileName) { var ext = Path.GetExtension(fileName).ToLower(); if (FormatManager.PdfExtensions().Contains(ext)) return FileType.Pdf; // pdf type for document extensions if (FormatManager.DocumentExtensions().Contains(ext)) return FileType.Word; // word type for document extensions if (FormatManager.SpreadsheetExtensions().Contains(ext)) return FileType.Cell; // cell type for spreadsheet extensions if (FormatManager.PresentationExtensions().Contains(ext)) return FileType.Slide; // slide type for presentation extensions return FileType.Word; // the default type is word } } public class EmptyTolerantStringEnumConverter : StringEnumConverter { public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.String && string.IsNullOrWhiteSpace(reader.Value.ToString())) return Activator.CreateInstance(objectType); return base.ReadJson(reader, objectType, existingValue, serializer); } } public class Format { public string Name { get; } [JsonConverter(typeof(EmptyTolerantStringEnumConverter))] public FileType Type { get; } public List Actions { get; } public List Convert { get; } public List Mime { get; } public Format(string name, FileType type, List actions, List convert, List mime) { Name = name; Type = type; Actions = actions; Convert = convert; Mime = mime; } public string Extension() { return "." + Name; } } public static class FormatManager { private static List cachedFormats; public static List FillableExtensions() { return Fillable() .Select(format => format.Extension()) .ToList(); } public static List Fillable() { return All() .Where(format => format.Actions.Contains("fill")) .ToList(); } public static List ViewableExtensions() { return Viewable() .Select(format => format.Extension()) .ToList(); } public static List Viewable() { return All() .Where(format => format.Actions.Contains("view")) .ToList(); } public static List EditableExtensions() { return Editable() .Select(format => format.Extension()) .ToList(); } public static List Editable() { return All() .Where(format => format.Actions.Contains("edit") || format.Actions.Contains("lossy-edit")) .ToList(); } public static List ConvertibleExtensions() { return Convertible() .Select(format => format.Extension()) .ToList(); } public static List Convertible() { return All() .Where(format => format.Actions.Contains("auto-convert")) .ToList(); } public static List SpreadsheetExtensions() { return Spreadsheets() .Select(format => format.Extension()) .ToList(); } public static List Spreadsheets() { return All() .Where(format => format.Type == FileType.Cell) .ToList(); } public static List PresentationExtensions() { return Presentations() .Select(format => format.Extension()) .ToList(); } public static List Presentations() { return All() .Where(format => format.Type == FileType.Slide) .ToList(); } public static List DocumentExtensions() { return Documents() .Select(format => format.Extension()) .ToList(); } public static List Documents() { return All() .Where(format => format.Type == FileType.Word) .ToList(); } public static List PdfExtensions() { return Pdfs() .Select(format => format.Extension()) .ToList(); } public static List Pdfs() { return All() .Where(format => format.Type == FileType.Pdf) .ToList(); } public static List AllExtensions() { return All() .Select(format => format.Extension()) .ToList(); } public static List All() { if (cachedFormats == null) { var path = GetPath(); var lines = File.ReadLines(path, Encoding.UTF8); var contents = string.Join(Environment.NewLine, lines); var formats = JsonConvert.DeserializeObject(contents); cachedFormats = formats.ToList(); } return cachedFormats; } private static string GetPath() { string path = Path.Combine(GetDirectory(), "onlyoffice-docs-formats.json"); if (File.Exists(path)) { return path; } else { throw new FileNotFoundException("The JSON file does not exist."); } } private static string GetDirectory() { string directory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets", "document-formats"); return Path.GetFullPath(directory); } } }