@@ -71,15 +71,24 @@ fun Route.addFiles() {
7171 }
7272 }
7373
74- val bytes = withIO { context.contentResolver.openInputStream(uri)?.buffered()?.use { it.readBytes() } }
75- if (bytes != null ) {
76- if (mimeType.isNotEmpty()) {
77- call.respondBytes(bytes, ContentType .parse(mimeType))
78- } else {
79- call.respondBytes(bytes, ContentType .Application .OctetStream )
80- }
81- } else {
74+ val input = withIO { context.contentResolver.openInputStream(uri)?.buffered() }
75+ if (input == null ) {
8276 call.respond(HttpStatusCode .NotFound )
77+ return @get
78+ }
79+ val contentType = if (mimeType.isNotEmpty()) ContentType .parse(mimeType) else ContentType .Application .OctetStream
80+ if (q[" dl" ] == " 1" ) {
81+ val fileName = (jsonName.ifEmpty { uri.lastPathSegment.orEmpty() }).urlEncode().replace(" +" , " %20" )
82+ if (fileName.isNotEmpty()) {
83+ call.response.header(" Access-Control-Expose-Headers" , " Content-Disposition" )
84+ call.response.header(
85+ " Content-Disposition" ,
86+ " attachment; filename=\" ${fileName} \" ; filename*=utf-8''${fileName} "
87+ )
88+ }
89+ }
90+ call.respondOutputStream(contentType) {
91+ input.use { it.copyTo(this ) }
8392 }
8493 } else if (path.startsWith(" pkgicon://" )) {
8594 val packageName = path.substring(10 )
@@ -147,11 +156,18 @@ fun Route.addFiles() {
147156 String (header.copyOfRange(8 , 12 )) in listOf (" heic" , " heix" , " hevc" , " hevx" , " avif" )
148157
149158 if (isHeif) {
150- val bytes = file.readBytes()
151- val bitmap = BitmapFactory .decodeByteArray(bytes, 0 , bytes.size)
152- val output = ByteArrayOutputStream ()
153- bitmap.compress(Bitmap .CompressFormat .PNG , 100 , output)
154- call.respondBytes(output.toByteArray(), ContentType .Image .PNG )
159+ val bitmap = withIO { BitmapFactory .decodeFile(path) }
160+ if (bitmap == null ) {
161+ call.respond(HttpStatusCode .NotFound )
162+ return @get
163+ }
164+ call.respondOutputStream(ContentType .Image .PNG ) {
165+ try {
166+ bitmap.compress(Bitmap .CompressFormat .PNG , 100 , this )
167+ } finally {
168+ bitmap.recycle()
169+ }
170+ }
155171 } else {
156172 call.respond(LocalFileContent (file, fileName.getContentType()))
157173 }
0 commit comments