Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
85739edf
Commit
85739edf
authored
Nov 12, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
参数处理
parent
34f714fc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
125 additions
and
57 deletions
+125
-57
YzgConvert.java
.../src/main/java/com/yanzuoguang/cloud/file/YzgConvert.java
+21
-0
YzgFileServiceImpl.java
...n/java/com/yanzuoguang/cloud/file/YzgFileServiceImpl.java
+104
-57
No files found.
yzg-util-file/src/main/java/com/yanzuoguang/cloud/file/YzgConvert.java
0 → 100644
View file @
85739edf
package
com
.
yanzuoguang
.
cloud
.
file
;
import
com.yanzuoguang.cloud.file.vo.YzgFileVideoImageItemReqVo
;
import
java.io.IOException
;
/**
* 转换文件接口
*
* @author 颜佐光
*/
public
interface
YzgConvert
{
/**
* 转换执行函数
*
* @param item 转换属性
* @param from 来源文件
* @param toFileTemp 目标文件
*/
void
run
(
YzgFileVideoImageItemReqVo
item
,
String
from
,
String
toFileTemp
)
throws
IOException
;
}
yzg-util-file/src/main/java/com/yanzuoguang/cloud/file/YzgFileServiceImpl.java
View file @
85739edf
...
@@ -129,53 +129,61 @@ public class YzgFileServiceImpl implements YzgFileService {
...
@@ -129,53 +129,61 @@ public class YzgFileServiceImpl implements YzgFileService {
// 先检查一遍参数
// 先检查一遍参数
for
(
YzgFileMoveItemReqVo
item
:
req
.
getList
())
{
for
(
YzgFileMoveItemReqVo
item
:
req
.
getList
())
{
if
(
StringHelper
.
isEmpty
(
item
.
getFrom
()))
{
checkMove
(
item
);
throw
new
CodeException
(
"移动来源路径不能为空"
);
}
}
if
(
StringHelper
.
isEmpty
(
item
.
getTo
()))
{
throw
new
CodeException
(
"移动目标路径不能为空"
);
}
checkFolder
(
item
.
getFrom
());
// 然后再移动
checkFolder
(
item
.
getTo
());
for
(
YzgFileMoveItemReqVo
item
:
req
.
getList
())
{
move
(
item
);
}
}
String
fullFrom
=
fileConfig
.
getServerFullPath
(
item
.
getFrom
());
private
void
checkMove
(
YzgFileMoveItemReqVo
item
)
{
String
fullTo
=
fileConfig
.
getServerFullPath
(
item
.
getTo
());
if
(
StringHelper
.
isEmpty
(
item
.
getFrom
()))
{
File
fileFrom
=
new
File
(
fullFrom
);
throw
new
CodeException
(
"移动来源路径不能为空"
);
File
fileTo
=
new
File
(
fullTo
);
}
if
(
StringHelper
.
isEmpty
(
item
.
getTo
()))
{
throw
new
CodeException
(
"移动目标路径不能为空"
);
}
if
(
fileFrom
.
exists
()
&&
fileTo
.
exists
())
{
checkFolder
(
item
.
getFrom
());
if
(
fileFrom
.
isFile
()
&&
fileTo
.
isDirectory
())
{
checkFolder
(
item
.
getTo
());
throw
new
CodeException
(
String
.
format
(
"不能用文件 %s 覆盖文件夹 %s"
,
item
.
getFrom
(),
item
.
getTo
()));
}
String
fullFrom
=
fileConfig
.
getServerFullPath
(
item
.
getFrom
());
String
fullTo
=
fileConfig
.
getServerFullPath
(
item
.
getTo
());
File
fileFrom
=
new
File
(
fullFrom
);
File
fileTo
=
new
File
(
fullTo
);
if
(
fileFrom
.
exists
()
&&
fileTo
.
exists
())
{
if
(
fileFrom
.
isFile
()
&&
fileTo
.
isDirectory
())
{
throw
new
CodeException
(
String
.
format
(
"不能用文件 %s 覆盖文件夹 %s"
,
item
.
getFrom
(),
item
.
getTo
()));
}
}
}
}
}
// 然后再移动
private
void
move
(
YzgFileMoveItemReqVo
item
)
{
for
(
YzgFileMoveItemReqVo
item
:
req
.
getList
())
{
String
fullFrom
=
fileConfig
.
getServerFullPath
(
item
.
getFrom
());
String
fullFrom
=
fileConfig
.
getServerFullPath
(
item
.
getFrom
());
String
fullTo
=
fileConfig
.
getServerFullPath
(
item
.
getTo
());
String
fullTo
=
fileConfig
.
getServerFullPath
(
item
.
getTo
());
File
fileFrom
=
new
File
(
fullFrom
);
File
fileFrom
=
new
File
(
fullFrom
);
File
fileTo
=
new
File
(
fullTo
);
File
fileTo
=
new
File
(
fullTo
);
// 防止另外一个线程移动中
// 防止另外一个线程移动中
if
(
fileFrom
.
exists
()
&&
fileTo
.
exists
())
{
if
(
fileFrom
.
exists
()
&&
fileTo
.
exists
())
{
if
(
fileFrom
.
isFile
()
&&
fileTo
.
isDirectory
())
{
if
(
fileFrom
.
isFile
()
&&
fileTo
.
isDirectory
())
{
throw
new
CodeException
(
String
.
format
(
"不能用文件 %s 覆盖文件夹 %s"
,
item
.
getFrom
(),
item
.
getTo
()));
throw
new
CodeException
(
String
.
format
(
"不能用文件 %s 覆盖文件夹 %s"
,
item
.
getFrom
(),
item
.
getTo
()));
}
}
else
if
(!
fileFrom
.
exists
())
{
continue
;
}
}
}
else
if
(!
fileFrom
.
exists
())
{
return
;
}
// 创建父文件夹
// 创建父文件夹
File
parentFile
=
fileTo
.
getParentFile
();
File
parentFile
=
fileTo
.
getParentFile
();
FileHelper
.
createDirectory
(
parentFile
);
FileHelper
.
createDirectory
(
parentFile
);
try
{
try
{
Files
.
move
(
Paths
.
get
(
fullFrom
),
Paths
.
get
(
fullTo
),
StandardCopyOption
.
REPLACE_EXISTING
);
Files
.
move
(
Paths
.
get
(
fullFrom
),
Paths
.
get
(
fullTo
),
StandardCopyOption
.
REPLACE_EXISTING
);
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
throw
new
CodeException
(
String
.
format
(
"移动文件 %s 到 %s 失败,原因:%s"
,
item
.
getFrom
(),
item
.
getTo
(),
ex
.
getMessage
()));
throw
new
CodeException
(
String
.
format
(
"移动文件 %s 到 %s 失败,原因:%s"
,
item
.
getFrom
(),
item
.
getTo
(),
ex
.
getMessage
()));
}
}
}
}
}
...
@@ -229,7 +237,13 @@ public class YzgFileServiceImpl implements YzgFileService {
...
@@ -229,7 +237,13 @@ public class YzgFileServiceImpl implements YzgFileService {
*/
*/
@Override
@Override
public
void
convertImage
(
YzgFileVideoImageReqVo
req
)
{
public
void
convertImage
(
YzgFileVideoImageReqVo
req
)
{
this
.
convert
(
req
,
FileHelper
.
FILE_TYPE_IMAGE
,
"图片"
,
new
YzgConvert
()
{
@Override
public
void
run
(
YzgFileVideoImageItemReqVo
item
,
String
from
,
String
toFileTemp
)
throws
IOException
{
// 压缩视频到临时文件
MediaHelper
.
compressPic
(
from
,
toFileTemp
,
item
.
getQuote
(),
item
.
getSize
());
}
});
}
}
/**
/**
...
@@ -239,7 +253,7 @@ public class YzgFileServiceImpl implements YzgFileService {
...
@@ -239,7 +253,7 @@ public class YzgFileServiceImpl implements YzgFileService {
*/
*/
@Override
@Override
public
void
convertVideoFirst
(
YzgFileConvertVideoFirstReqVo
req
)
{
public
void
convertVideoFirst
(
YzgFileConvertVideoFirstReqVo
req
)
{
}
}
/**
/**
...
@@ -249,22 +263,47 @@ public class YzgFileServiceImpl implements YzgFileService {
...
@@ -249,22 +263,47 @@ public class YzgFileServiceImpl implements YzgFileService {
*/
*/
@Override
@Override
public
void
convertVideo
(
YzgFileVideoImageReqVo
req
)
{
public
void
convertVideo
(
YzgFileVideoImageReqVo
req
)
{
this
.
convert
(
req
,
FileHelper
.
FILE_TYPE_VIDEO
,
"视频"
,
new
YzgConvert
()
{
@Override
public
void
run
(
YzgFileVideoImageItemReqVo
item
,
String
from
,
String
toFileTemp
)
throws
IOException
{
// 压缩视频的参数
MediaParameter
parameter
=
new
MediaParameter
(
item
.
getSize
(),
item
.
getQuote
());
// 设置最小宽度,指的是宽度和高度中的大值,宽度>高度
parameter
.
setMinVideoWidth
(
StringHelper
.
getFirst
(
item
.
getWidth
(),
parameter
.
getMinVideoWidth
()));
// 设置最小高度,指的是宽度和高度中的小值,宽度>高度
parameter
.
setMinVideoHeight
(
StringHelper
.
getFirst
(
item
.
getHeight
(),
parameter
.
getMinVideoHeight
()));
// 设置视频最小比特率
parameter
.
setMinVideoBitRate
(
StringHelper
.
getFirst
(
item
.
getBitrate
(),
parameter
.
getMinVideoBitRate
()));
// 压缩视频到临时文件
MediaHelper
.
zipVideoMp4
(
from
,
toFileTemp
,
parameter
);
}
});
}
/**
* 转换文件函数
*
* @param req
* @param type
* @param tag
* @param run
*/
private
void
convert
(
YzgFileVideoImageReqVo
req
,
int
type
,
String
tag
,
YzgConvert
run
)
{
List
<
YzgFileVideoImageItemReqVo
>
toFiles
=
req
.
getList
();
List
<
YzgFileVideoImageItemReqVo
>
toFiles
=
req
.
getList
();
if
(
toFiles
==
null
||
toFiles
.
isEmpty
())
{
if
(
toFiles
==
null
||
toFiles
.
isEmpty
())
{
return
;
return
;
}
}
// 先循环检测参数
checkFolder
(
req
.
getFrom
());
// 转换文件基础处理
for
(
YzgFileVideoImageItemReqVo
item
:
req
.
getList
())
{
convertBase
(
req
);
checkFolder
(
item
.
getTo
());
}
// 来源文件路径
// 来源文件路径
String
from
=
fileConfig
.
getServerFullPath
(
req
.
getFrom
());
String
from
=
fileConfig
.
getServerFullPath
(
req
.
getFrom
());
YzgFileBaseVo
fromInfo
=
new
YzgFileBaseVo
();
YzgFileBaseVo
fromInfo
=
new
YzgFileBaseVo
();
fileConfig
.
init
(
fromInfo
,
from
);
fileConfig
.
init
(
fromInfo
,
from
);
if
(
fromInfo
.
getType
()
!=
FileHelper
.
FILE_TYPE_VIDEO
)
{
if
(
fromInfo
.
getType
()
!=
type
)
{
throw
new
CodeException
(
"请传入
视频
文件"
);
throw
new
CodeException
(
"请传入
"
+
tag
+
"
文件"
);
}
}
// 循环转换所有文件
// 循环转换所有文件
...
@@ -272,14 +311,6 @@ public class YzgFileServiceImpl implements YzgFileService {
...
@@ -272,14 +311,6 @@ public class YzgFileServiceImpl implements YzgFileService {
String
to
=
fileConfig
.
getServerFullPath
(
item
.
getTo
());
String
to
=
fileConfig
.
getServerFullPath
(
item
.
getTo
());
// 先压缩到临时文件
// 先压缩到临时文件
String
toFileTemp
=
to
+
StringHelper
.
getNewID
()
+
".tmp"
;
String
toFileTemp
=
to
+
StringHelper
.
getNewID
()
+
".tmp"
;
MediaParameter
parameter
=
new
MediaParameter
(
item
.
getSize
(),
item
.
getQuote
());
// 设置最小宽度,指的是宽度和高度中的大值,宽度>高度
parameter
.
setMinVideoWidth
(
StringHelper
.
getFirst
(
item
.
getWidth
(),
parameter
.
getMinVideoWidth
()));
// 设置最小高度,指的是宽度和高度中的小值,宽度>高度
parameter
.
setMinVideoHeight
(
StringHelper
.
getFirst
(
item
.
getHeight
(),
parameter
.
getMinVideoHeight
()));
// 设置视频最小比特率
parameter
.
setMinVideoBitRate
(
StringHelper
.
getFirst
(
item
.
getBitrate
(),
parameter
.
getMinVideoBitRate
()));
// 将临时文件改成具体的文件
// 将临时文件改成具体的文件
File
fileTemp
=
new
File
(
toFileTemp
);
File
fileTemp
=
new
File
(
toFileTemp
);
File
file
=
new
File
(
to
);
File
file
=
new
File
(
to
);
...
@@ -287,8 +318,8 @@ public class YzgFileServiceImpl implements YzgFileService {
...
@@ -287,8 +318,8 @@ public class YzgFileServiceImpl implements YzgFileService {
if
(
fileTemp
.
exists
())
{
if
(
fileTemp
.
exists
())
{
fileTemp
.
delete
();
fileTemp
.
delete
();
}
}
//
压缩视频到临时
文件
//
转换
文件
MediaHelper
.
zipVideoMp4
(
from
,
toFileTemp
,
parameter
);
run
.
run
(
item
,
from
,
toFileTemp
);
// 判断文件是否存在,不存在则更改临时文件名
// 判断文件是否存在,不存在则更改临时文件名
if
(
file
.
exists
())
{
if
(
file
.
exists
())
{
file
.
delete
();
file
.
delete
();
...
@@ -306,6 +337,22 @@ public class YzgFileServiceImpl implements YzgFileService {
...
@@ -306,6 +337,22 @@ public class YzgFileServiceImpl implements YzgFileService {
}
}
}
}
private
void
convertBase
(
YzgFileVideoImageReqVo
req
)
{
// 先循环检测参数
checkFolder
(
req
.
getFrom
());
for
(
YzgFileVideoImageItemReqVo
item
:
req
.
getList
())
{
checkFolder
(
item
.
getTo
());
}
// 移动文件,并且确定只移动一次
if
(!
StringHelper
.
isEmpty
(
req
.
getTo
()))
{
checkMove
(
req
);
move
(
req
);
req
.
setFrom
(
req
.
getTo
());
req
.
setTo
(
StringHelper
.
EMPTY
);
}
}
/**
/**
* 获取行记的显示的图片
* 获取行记的显示的图片
*
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment