Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
2a80d8a5
Commit
2a80d8a5
authored
Dec 31, 2019
by
Freedom
Committed by
Him188
Dec 31, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add ExternalImageAndroid.kt
parent
f632c15f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
102 additions
and
0 deletions
+102
-0
mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/ExternalImageAndroid.kt
...Main/kotlin/net/mamoe/mirai/utils/ExternalImageAndroid.kt
+102
-0
No files found.
mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/ExternalImageAndroid.kt
0 → 100644
View file @
2a80d8a5
@
file
:
Suppress
(
"EXPERIMENTAL_API_USAGE"
)
package
net.mamoe.mirai.utils
import
android.graphics.BitmapFactory
import
io.ktor.util.asStream
import
kotlinx.coroutines.Dispatchers.IO
import
kotlinx.coroutines.withContext
import
kotlinx.io.core.Input
import
kotlinx.io.core.IoBuffer
import
kotlinx.io.core.copyTo
import
kotlinx.io.errors.IOException
import
kotlinx.io.streams.asInput
import
kotlinx.io.streams.asOutput
import
java.io.File
import
java.io.InputStream
import
java.net.URL
/*
* 将各类型图片容器转为 [ExternalImage]
*/
/**
* 读取文件头识别图片属性, 然后构造 [ExternalImage]
*/
@Throws
(
IOException
::
class
)
fun
File
.
toExternalImage
():
ExternalImage
{
val
input
=
BitmapFactory
.
decodeFile
(
this
.
absolutePath
)
checkNotNull
(
input
)
{
"Unable to read file(path=${this.path}), BitmapFactory.decodeFile returned null"
}
return
ExternalImage
(
width
=
input
.
width
,
height
=
input
.
height
,
md5
=
this
.
inputStream
().
use
{
it
.
md5
()
},
imageFormat
=
this
.
nameWithoutExtension
,
input
=
this
.
inputStream
().
asInput
(
IoBuffer
.
Pool
),
inputSize
=
this
.
length
()
)
}
/**
* 在 [IO] 中进行 [File.toExternalImage]
*/
@Suppress
(
"unused"
)
suspend
fun
File
.
suspendToExternalImage
():
ExternalImage
=
withContext
(
IO
)
{
toExternalImage
()
}
/**
* 下载文件到临时目录然后调用 [File.toExternalImage]
*/
@Throws
(
IOException
::
class
)
fun
URL
.
toExternalImage
():
ExternalImage
{
val
file
=
createTempFile
().
apply
{
deleteOnExit
()
}
file
.
outputStream
().
asOutput
().
use
{
output
->
openStream
().
asInput
().
use
{
input
->
input
.
copyTo
(
output
)
}
}
return
file
.
toExternalImage
()
}
/**
* 在 [IO] 中进行 [URL.toExternalImage]
*/
@Suppress
(
"unused"
)
suspend
fun
URL
.
suspendToExternalImage
():
ExternalImage
=
withContext
(
IO
)
{
toExternalImage
()
}
/**
* 保存为临时文件然后调用 [File.toExternalImage]
*/
@Throws
(
IOException
::
class
)
fun
InputStream
.
toExternalImage
():
ExternalImage
{
val
file
=
createTempFile
().
apply
{
deleteOnExit
()
}
file
.
outputStream
().
asOutput
().
use
{
this
.
asInput
().
copyTo
(
it
)
}
this
.
close
()
return
file
.
toExternalImage
()
}
/**
* 在 [IO] 中进行 [InputStream.toExternalImage]
*/
@Suppress
(
"unused"
)
suspend
fun
InputStream
.
suspendToExternalImage
():
ExternalImage
=
withContext
(
IO
)
{
toExternalImage
()
}
/**
* 保存为临时文件然后调用 [File.toExternalImage]
*/
@Throws
(
IOException
::
class
)
fun
Input
.
toExternalImage
():
ExternalImage
{
val
file
=
createTempFile
().
apply
{
deleteOnExit
()
}
file
.
outputStream
().
asOutput
().
use
{
this
.
asStream
().
asInput
().
copyTo
(
it
)
}
return
file
.
toExternalImage
()
}
/**
* 在 [IO] 中进行 [Input.toExternalImage]
*/
@Suppress
(
"unused"
)
suspend
fun
Input
.
suspendToExternalImage
():
ExternalImage
=
withContext
(
IO
)
{
toExternalImage
()
}
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