mirror of
https://github.com/ONLYOFFICE/document-server-integration.git
synced 2026-02-10 18:05:10 +08:00
java: fixed FormatManager
This commit is contained in:
@ -25,9 +25,11 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
@ -35,69 +37,49 @@ import com.google.gson.JsonSyntaxException;
|
||||
import entities.FileType;
|
||||
|
||||
public final class FormatManager {
|
||||
public List<String> fillableExtensions() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
|
||||
private List<Format> formats;
|
||||
|
||||
public FormatManager() {
|
||||
formats = this.all();
|
||||
}
|
||||
|
||||
|
||||
public List<Format> getFormats() {
|
||||
return this.formats;
|
||||
}
|
||||
|
||||
public List<Format> getFormatsByAction(String action) {
|
||||
return this
|
||||
.fillable()
|
||||
.all()
|
||||
.stream()
|
||||
.filter(format -> format.getActions().contains(action))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<String> fillableExtensions() {
|
||||
return this
|
||||
.getFormatsByAction("fill")
|
||||
.stream()
|
||||
.map(format -> format.extension())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Format> fillable() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
public List<String> viewableExtensions() {
|
||||
return this
|
||||
.all()
|
||||
.stream()
|
||||
.filter(format -> format.getActions().contains("fill"))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<String> viewableExtensions() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
return this
|
||||
.viewable()
|
||||
.getFormatsByAction("view")
|
||||
.stream()
|
||||
.map(format -> format.extension())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Format> viewable() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
return this
|
||||
.all()
|
||||
.stream()
|
||||
.filter(format -> format.getActions().contains("view"))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<String> editableExtensions() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
return this
|
||||
.editable()
|
||||
.stream()
|
||||
public List<String> editableExtensions() {
|
||||
return Stream.of(this.getFormatsByAction("edit"), this.getFormatsByAction("edit"))
|
||||
.flatMap(x -> x.stream())
|
||||
.map(format -> format.extension())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Format> editable() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
return this
|
||||
.all()
|
||||
.stream()
|
||||
.filter(format -> (
|
||||
format.getActions().contains("edit")
|
||||
|| format.getActions().contains("lossy-edit")
|
||||
))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<String> convertibleExtensions() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
@ -112,7 +94,7 @@ public final class FormatManager {
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
return this
|
||||
.all()
|
||||
.formats
|
||||
.stream()
|
||||
.filter(format -> (
|
||||
format.getType() == FileType.Cell && format.getConvert().contains("xlsx")
|
||||
@ -122,85 +104,19 @@ public final class FormatManager {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<String> spreadsheetExtensions() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
return this
|
||||
.spreadsheets()
|
||||
.stream()
|
||||
.map(format -> format.extension())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
private List<Format> all() {
|
||||
try {
|
||||
Path path = this.file();
|
||||
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
|
||||
String contents = String.join(System.lineSeparator(), lines);
|
||||
Gson gson = new Gson();
|
||||
Format[] formats = gson.fromJson(contents, Format[].class);
|
||||
return Arrays.asList(formats);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
public List<Format> spreadsheets() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
return this
|
||||
.all()
|
||||
.stream()
|
||||
.filter(format -> format.getType() == FileType.Cell)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<String> presentationExtensions() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
return this
|
||||
.presentations()
|
||||
.stream()
|
||||
.map(format -> format.extension())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Format> presentations() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
return this
|
||||
.all()
|
||||
.stream()
|
||||
.filter(format -> format.getType() == FileType.Slide)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<String> documentExtensions() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
return this
|
||||
.documents()
|
||||
.stream()
|
||||
.map(format -> format.extension())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Format> documents() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
return this
|
||||
.all()
|
||||
.stream()
|
||||
.filter(format -> format.getType() == FileType.Word)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<String> allExtensions() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
return this
|
||||
.all()
|
||||
.stream()
|
||||
.map(format -> format.extension())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Format> all() throws URISyntaxException,
|
||||
IOException,
|
||||
JsonSyntaxException {
|
||||
Path path = this.file();
|
||||
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
|
||||
String contents = String.join(System.lineSeparator(), lines);
|
||||
Gson gson = new Gson();
|
||||
Format[] formats = gson.fromJson(contents, Format[].class);
|
||||
return Arrays.asList(formats);
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
private Path file() throws URISyntaxException {
|
||||
|
||||
@ -19,10 +19,12 @@
|
||||
package helpers;
|
||||
|
||||
import entities.FileType;
|
||||
import format.Format;
|
||||
import format.FormatManager;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public final class FileUtility {
|
||||
@ -33,19 +35,12 @@ public final class FileUtility {
|
||||
// get file type
|
||||
public static FileType getFileType(final String fileName) {
|
||||
String ext = getFileExtension(fileName).toLowerCase();
|
||||
List<Format> formats = FileUtility.formatManager.getFormats();
|
||||
|
||||
try {
|
||||
if (FileUtility.formatManager.documentExtensions().contains(ext)) {
|
||||
return FileType.Word;
|
||||
for (Format format : formats) {
|
||||
if (format.getName().equals(ext)) {
|
||||
return format.getType();
|
||||
}
|
||||
if (FileUtility.formatManager.spreadsheetExtensions().contains(ext)) {
|
||||
return FileType.Cell;
|
||||
}
|
||||
if (FileUtility.formatManager.presentationExtensions().contains(ext)) {
|
||||
return FileType.Slide;
|
||||
}
|
||||
} catch (Exception error) {
|
||||
error.printStackTrace();
|
||||
}
|
||||
|
||||
// default file type is word
|
||||
|
||||
Reference in New Issue
Block a user