Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
BYLAppRobot
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
liuyang
BYLAppRobot
Commits
b1be290d
Commit
b1be290d
authored
Nov 26, 2018
by
liuyang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
APK图片抓取识别及轮播流地址抓取, ftp上传增加超时检测机制
#BYLSERVER-1438
parent
80c21a83
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
98 additions
and
4 deletions
+98
-4
ImageUploadTask.kt
...rc/main/java/com/duolebo/blyrobot/data/ImageUploadTask.kt
+97
-1
AppUtil.kt
app/src/main/java/com/duolebo/blyrobot/util/AppUtil.kt
+1
-3
No files found.
app/src/main/java/com/duolebo/blyrobot/data/ImageUploadTask.kt
View file @
b1be290d
...
...
@@ -2,11 +2,15 @@ package com.duolebo.blyrobot.data
import
android.content.Context
import
android.util.Log
import
com.duolebo.blyrobot.tools.FtpManager
import
com.duolebo.blyrobot.util.AppUtil
import
com.duolebo.blyrobot.util.Config
import
net.gotev.uploadservice.*
import
net.gotev.uploadservice.ftp.FTPUploadRequest
import
net.gotev.uploadservice.ftp.UnixPermissions
import
java.io.File
import
java.util.*
import
kotlin.collections.ArrayList
/**
* 图片上传任务
...
...
@@ -24,6 +28,10 @@ class ImageUploadTask {
var
isRunning
=
false
private
var
context
:
Context
private
var
uploadCheckTimer
:
Timer
?
=
null
private
var
isUploadChecking
=
false
private
var
checkDelay
=
5
*
60
*
1000L
private
var
checkCount
=
0
constructor
(
context
:
Context
)
{
this
.
context
=
context
...
...
@@ -45,6 +53,9 @@ class ImageUploadTask {
if
(
isUploadRunning
())
return
false
if
(
this
.
isUploadChecking
)
return
false
return
true
}
...
...
@@ -58,8 +69,85 @@ class ImageUploadTask {
}
fun
start
()
{
if
(
this
.
uploadImages
.
isEmpty
())
{
uploadTaskListener
?.
onComplete
()
return
}
this
.
isRunning
=
true
Thread
{
uploadImage
()
}.
start
()
// 预计每张图片上传时间是3秒
startUploadCheck
(
3
*
1000L
*
this
.
uploadImages
.
size
)
}
// 由于ftp库存在bug,这里启动一个定时器进行超时上传检测
private
fun
startUploadCheck
(
delay
:
Long
)
{
Log
.
i
(
TAG
,
"startUploadCheck with delay: $delay"
)
this
.
checkDelay
=
delay
uploadCheckTimer
?.
cancel
()
if
(
this
.
checkCount
>=
3
)
{
Log
.
i
(
TAG
,
"check time out, quit this job"
)
uploadTaskListener
?.
onComplete
()
return
}
uploadCheckTimer
=
Timer
()
uploadCheckTimer
!!
.
schedule
(
object
:
TimerTask
()
{
override
fun
run
()
{
checkUpload
()
}
},
delay
)
this
.
checkCount
++
}
private
fun
checkUpload
()
{
Log
.
i
(
TAG
,
"check image upload"
)
if
(
isUploadRunning
())
{
startUploadCheck
(
this
.
checkDelay
/
3
)
return
}
this
.
isUploadChecking
=
true
val
lastUploadPos
=
getLastUploadImagePos
()
Log
.
i
(
TAG
,
"get last upload pos:$lastUploadPos"
)
val
size
=
this
.
uploadImages
.
size
// 移除所有已经上传的图片
val
reUploadImages
=
this
.
uploadImages
.
subList
(
lastUploadPos
,
size
-
1
)
this
.
uploadImages
.
removeAll
(
reUploadImages
)
this
.
reUpload
=
true
this
.
isUploadChecking
=
false
}
// 通过检查url是否存在找到最后上传图片的位置
private
fun
getLastUploadImagePos
():
Int
{
val
res
:
Int
val
size
=
this
.
uploadImages
.
size
var
checkPos
=
size
/
2
var
halfPos
=
checkPos
while
(
true
)
{
val
exist
=
AppUtil
.
existsUrl
(
this
.
uploadImages
[
checkPos
])
if
(
exist
)
{
checkPos
+=
halfPos
/
2
}
else
{
checkPos
-=
halfPos
/
2
}
halfPos
/=
2
if
(
halfPos
==
0
)
{
res
=
if
(
exist
)
checkPos
else
checkPos
-
1
break
}
}
return
res
}
private
fun
uploadImage
()
{
...
...
@@ -74,13 +162,21 @@ class ImageUploadTask {
.
setDelegate
(
uploadImageCallback
())
.
setMaxRetries
(
4
)
val
unExistFiles
=
ArrayList
<
String
>()
this
.
uploadImages
.
forEach
{
val
uploadFile
=
File
(
it
)
if
(
uploadFile
.
exists
())
{
uploadRequest
.
addFileToUpload
(
it
,
Config
.
instance
.
getFtpRemotePath
()
+
uploadFile
.
name
)
}
else
{
unExistFiles
.
add
(
it
)
}
}
// 删除不存在的图片记录
if
(!
unExistFiles
.
isEmpty
())
{
this
.
uploadImages
.
removeAll
(
unExistFiles
)
}
this
.
uploadId
=
uploadRequest
.
startUpload
()
Log
.
i
(
TAG
,
"upload id $uploadId"
)
}
catch
(
exc
:
Exception
)
{
...
...
@@ -90,6 +186,7 @@ class ImageUploadTask {
private
fun
uploadComplete
(
errorCount
:
Int
)
{
Log
.
i
(
TAG
,
"upload errorCount: $errorCount"
)
uploadCheckTimer
?.
cancel
()
this
.
isRunning
=
false
if
(
errorCount
>
0
&&
errorCount
>
this
.
uploadImages
.
size
/
3
)
{
this
.
reUpload
=
true
...
...
@@ -104,7 +201,6 @@ class ImageUploadTask {
}
uploadTaskListener
?.
onComplete
()
}
/**
...
...
app/src/main/java/com/duolebo/blyrobot/util/AppUtil.kt
View file @
b1be290d
...
...
@@ -23,8 +23,6 @@ import android.R.attr.versionName
import
android.content.pm.PackageInfo
object
AppUtil
{
fun
deleteFile
(
file
:
File
?,
fileFilter
:
FileFilter
)
{
...
...
@@ -229,7 +227,7 @@ object AppUtil {
}
}
fun
exists
(
url
:
String
):
Boolean
{
fun
exists
Url
(
url
:
String
):
Boolean
{
return
try
{
HttpURLConnection
.
setFollowRedirects
(
false
)
val
con
=
URL
(
url
).
openConnection
()
as
HttpURLConnection
...
...
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