//
|
// Source code recreated from a .class file by IntelliJ IDEA
|
// (powered by Fernflower decompiler)
|
//
|
|
package com.product.data.sync.service.media;
|
|
import com.product.core.service.support.AbstractBaseService;
|
import org.springframework.stereotype.Service;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.text.Collator;
|
import java.util.Arrays;
|
import java.util.Comparator;
|
import java.util.HashMap;
|
import java.util.LinkedHashMap;
|
import java.util.Locale;
|
import java.util.Map;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
@Service
|
public class GdMediaUtil extends AbstractBaseService{
|
private static String root = "D:/Media/";
|
private static String testString = "^(\\d{0,4}`)|(\\d{0,3}\\^\\d{0,4}`)";
|
private static String startWith = "FE#ENC#";
|
private static String key = "fe520";
|
public String templete = "TemplateOfCreate";
|
public String taohongTemplete = "TemplateOfTaohong";
|
public String faceTempLate = "FaceTempLate";
|
static Encryption encryption;
|
|
static {
|
encryption = new Encryption(key);
|
root = SysSetup.getMediaParameter("MEDIA_PATH");
|
if (root == null) {
|
root = "D:\\OA\\FEGOV\\Media\\";
|
}
|
|
}
|
|
public GdMediaUtil() {
|
}
|
|
private String getPath(String code) {
|
if (code.length() < 15) {
|
return null;
|
} else {
|
code = code.substring(0, 14);
|
return code.substring(0, 4) + "/" + code.substring(4, 6) + "/" + code.substring(6, 8) + "/" + code;
|
}
|
}
|
|
private String getRoot(String code) {
|
String out = root + "/" + this.getPath(code) + "/";
|
this.mvdir(out);
|
return out;
|
}
|
|
public static boolean mvdir(String path) {
|
if (path != null && !path.equals("")) {
|
path = path.replace(File.separatorChar, '/');
|
boolean allFolder = false;
|
if (path.endsWith("/")) {
|
allFolder = true;
|
}
|
|
String[] folders = path.split("/");
|
boolean success = true;
|
String root = null;
|
int index = allFolder ? folders.length : folders.length - 1;
|
|
for(int i = 0; i < index; ++i) {
|
if (root == null) {
|
root = folders[0];
|
} else {
|
root = root + "/" + folders[i];
|
}
|
|
File file = new File(root);
|
if (!file.exists()) {
|
success = file.mkdir();
|
}
|
|
if (!success) {
|
return success;
|
}
|
}
|
|
return success;
|
} else {
|
return false;
|
}
|
}
|
|
public File[] getMediaTemplete() {
|
return this.getMediaTemplete((String)null);
|
}
|
|
public File[] getMediaTemplete(String parent) {
|
File file = null;
|
if (parent != null && !parent.equals("")) {
|
file = new File(parent);
|
} else {
|
file = new File(root + "/" + this.templete);
|
}
|
|
if (!file.exists()) {
|
file.mkdir();
|
}
|
|
return file.listFiles();
|
}
|
|
public String getParent(String filepath) {
|
if (filepath == null) {
|
return null;
|
} else {
|
File file = new File(root + "/" + this.templete);
|
File file1 = new File(filepath);
|
return file.equals(file1) ? null : file1.getParent().replaceAll("\\\\", "/");
|
}
|
}
|
|
public String getRelationDir(String filepath) {
|
if (filepath == null) {
|
return "";
|
} else {
|
filepath = filepath.replaceAll("\\\\", "/");
|
int i = filepath.indexOf(this.templete);
|
return i < 0 ? filepath : filepath.substring(i + this.templete.length() + 1);
|
}
|
}
|
|
public File[] getTaohongTemplete() {
|
return this.getTaohongTemplete("");
|
}
|
|
public File[] getTaohongTemplete(String subfloder) {
|
File file = new File(root + "/" + this.taohongTemplete + "/" + subfloder);
|
if (!file.exists()) {
|
file.mkdir();
|
}
|
|
File[] allfile = file.listFiles();
|
this.sort(allfile);
|
return allfile;
|
}
|
|
private void sort(File[] files) {
|
Arrays.sort(files, new Comparator<File>() {
|
public int compare(File arg0, File arg1) {
|
if (arg0.isDirectory() && arg1.isDirectory()) {
|
return GdMediaUtil.this.compareByName(arg0, arg1);
|
} else if (arg0.isDirectory() && !arg1.isDirectory()) {
|
return -1;
|
} else {
|
return !arg0.isDirectory() && arg1.isDirectory() ? 1 : GdMediaUtil.this.compareByName(arg0, arg1);
|
}
|
}
|
});
|
}
|
|
private int compareByName(File object1, File object2) {
|
Comparator<Object> cmp = Collator.getInstance(Locale.CHINA);
|
return cmp.compare(object1.getName(), object2.getName());
|
}
|
|
public File[] getFaceTempLate() {
|
File file = new File(root + "/" + this.faceTempLate + "/");
|
if (!file.exists()) {
|
file.mkdir();
|
}
|
|
return file.listFiles();
|
}
|
|
public File getTemplateFile(String name, String path) {
|
File file = null;
|
if (path != null && path.length() > 0) {
|
file = new File(root + "/" + path + "/" + name);
|
} else {
|
file = new File(root + "/" + this.templete + "/" + name);
|
}
|
|
return file;
|
}
|
|
public static void main(String[] args) {
|
GdMediaUtil gdMediaUtil = new GdMediaUtil();
|
gdMediaUtil.getMediaFiles("2022032535010101");
|
}
|
|
public Map<String, File> getMediaFiles(String code) {
|
int c = this.getVersion(code);
|
File file = new File(this.getRoot(code));
|
if (file.exists()) {
|
File[] files = file.listFiles();
|
Map<String, Integer> map = new HashMap();
|
Map<String, File> filemap = new LinkedHashMap();
|
|
for(int i = 0; i < files.length; ++i) {
|
GdMediaUtil.MediaFile mediaFile = new GdMediaUtil.MediaFile(files[i].getAbsolutePath());
|
if (!mediaFile.isDirectory()) {
|
int version = mediaFile.getVersion();
|
if (version <= c) {
|
String filename = mediaFile.getName();
|
if (filename.indexOf(".") != 0) {
|
Integer integer = (Integer)map.get(filename);
|
if (integer == null) {
|
map.put(filename, new Integer(version));
|
if (!mediaFile.isDelete()) {
|
filemap.put(filename, mediaFile);
|
}
|
} else if (integer <= version) {
|
map.put(filename, new Integer(version));
|
if (!mediaFile.isDelete()) {
|
if (integer == version) {
|
GdMediaUtil.MediaFile omediaFile = (GdMediaUtil.MediaFile)filemap.get(filename);
|
if (omediaFile != null && omediaFile.lastModified() < mediaFile.lastModified()) {
|
filemap.put(filename, mediaFile);
|
}
|
} else {
|
filemap.put(filename, mediaFile);
|
}
|
} else if (integer != version) {
|
filemap.remove(filename);
|
}
|
}
|
}
|
}
|
}
|
}
|
return filemap;
|
} else {
|
return null;
|
}
|
}
|
|
public boolean deleteMediaFile(String code, String name) {
|
File file = new File(this.getRoot(code));
|
int c = this.getVersion(code);
|
if (file.exists()) {
|
File[] files = file.listFiles();
|
int maxVersion = 0;
|
GdMediaUtil.MediaFile currentMediaFile = null;
|
|
for(int i = 0; i < files.length; ++i) {
|
String filename = files[i].getName();
|
if (filename.indexOf(".") != 0) {
|
GdMediaUtil.MediaFile mediaFile = new GdMediaUtil.MediaFile(files[i].getAbsolutePath());
|
if (!mediaFile.isDelete() && mediaFile.getName().equals(name)) {
|
int thisv = mediaFile.getVersion();
|
if (thisv <= c && maxVersion < thisv) {
|
maxVersion = thisv;
|
currentMediaFile = mediaFile;
|
}
|
}
|
}
|
}
|
|
if (currentMediaFile == null) {
|
return true;
|
} else if (maxVersion == c) {
|
return currentMediaFile.delete();
|
} else {
|
return currentMediaFile.delete(c);
|
}
|
} else {
|
return true;
|
}
|
}
|
|
public File createMediaFile(String code, String name) {
|
int c = this.getVersion(code);
|
name = this.encryptFileName(name);
|
name = c + "`" + name;
|
File file = new GdMediaUtil.MediaFile(this.getRoot(code) + "/" + name);
|
return file;
|
}
|
|
|
|
public String encryptFileName(String fileName) {
|
String version = "";
|
String nfileName = "";
|
Pattern p = Pattern.compile(testString);
|
Matcher m = p.matcher(fileName);
|
if (m.find()) {
|
version = m.group();
|
nfileName = fileName.substring(m.end());
|
} else {
|
nfileName = fileName;
|
}
|
|
if (nfileName.startsWith(startWith)) {
|
return fileName;
|
} else {
|
nfileName = startWith + encryption.encryptString(nfileName, true);
|
return version + nfileName;
|
}
|
}
|
|
public String decryptFileName(String fileName) {
|
String version = "";
|
String nfileName = "";
|
Pattern p = Pattern.compile(testString);
|
Matcher m = p.matcher(fileName);
|
if (m.find()) {
|
version = m.group();
|
nfileName = fileName.substring(m.end());
|
} else {
|
nfileName = fileName;
|
}
|
if (nfileName.startsWith(startWith)) {
|
nfileName = encryption.decryptionString(nfileName.substring(startWith.length()), true);
|
return version + nfileName;
|
} else {
|
return fileName;
|
}
|
}
|
|
public String getRoot() {
|
return root;
|
}
|
|
public void setRoot(String root) {
|
GdMediaUtil.root = root;
|
}
|
|
private int getVersion(String code) {
|
if (code.length() < 15) {
|
return 0;
|
} else {
|
String ban = code.substring(14);
|
int c = 0;
|
|
try {
|
c = this.converFiftyInt(ban);
|
} catch (Exception var5) {
|
}
|
|
return c;
|
}
|
}
|
|
public static int converFiftyInt(String iData) {
|
int value = 0;
|
|
for(int i = iData.length() - 1; i > -1; --i) {
|
value = (int)((double)value + (double)"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()[]{}_+-=!@#$%&".indexOf(iData.charAt(i)) * Math.pow((double)"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()[]{}_+-=!@#$%&".length(), (double)(iData.length() - i - 1)));
|
}
|
|
return value;
|
}
|
public String getTemplete() {
|
return this.templete;
|
}
|
|
public void setTemplete(String templete) {
|
this.templete = templete;
|
}
|
|
class MediaFile extends File {
|
private static final long serialVersionUID = -5914321855770345214L;
|
|
public MediaFile(String path) {
|
super(path);
|
}
|
|
public String getName() {
|
String name = this.getRealName();
|
name = GdMediaUtil.this.decryptFileName(name);
|
return name.replaceFirst(GdMediaUtil.testString, "");
|
}
|
|
public String getRealName() {
|
return super.getName();
|
}
|
|
public int getVersion() {
|
String name = this.getRealName();
|
int index = name.indexOf("`");
|
String tempCode = name.substring(0, index);
|
int index1 = tempCode.indexOf("^");
|
if (index1 > 0) {
|
tempCode = tempCode.substring(0, index1);
|
}
|
|
boolean var5 = false;
|
|
try {
|
int v = Integer.parseInt(tempCode);
|
return v;
|
} catch (Exception var7) {
|
return 0;
|
}
|
}
|
|
public boolean isDelete() {
|
return this.getRealName().indexOf("^") > -1;
|
}
|
|
public boolean delete() {
|
return this.delete(this.getVersion());
|
}
|
|
public boolean delete(int version) {
|
File newFile;
|
if (version == 1) {
|
newFile = new File(this.getParentFile().getAbsolutePath() + "/" + this.getRealName());
|
return newFile.exists() ? newFile.delete() : false;
|
} else {
|
newFile = new File(this.getParentFile().getAbsolutePath() + "/" + version + "^" + this.getRealName());
|
|
try {
|
if (version != this.getVersion()) {
|
return newFile.createNewFile();
|
}
|
|
if (!newFile.exists()) {
|
return this.renameTo(newFile);
|
}
|
|
newFile = new File(this.getAbsolutePath());
|
if (newFile.isFile() && newFile.exists()) {
|
newFile.delete();
|
}
|
} catch (IOException var4) {
|
}
|
|
return false;
|
}
|
}
|
|
public boolean rename(String name) {
|
String oldName = this.getRealName();
|
int start = oldName.indexOf("`");
|
if (start < 0) {
|
return false;
|
} else {
|
int stop = oldName.lastIndexOf(".");
|
if (stop < 0) {
|
stop = oldName.length();
|
}
|
|
String newName = oldName.substring(0, start + 1) + name + oldName.substring(stop);
|
File newFile = new File(this.getParentFile().getAbsolutePath() + "/" + newName);
|
return this.renameTo(newFile);
|
}
|
}
|
}
|
}
|