From 697d01882ba8b1dbb85484ba3bf6e810e32448fc Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 24 Dec 2018 01:47:03 -0500 Subject: Enhancement/ktlint (#1259) * Add spotless * Reformat code * Apply license header * Add remaining license headers --- .../pitchedapps/frost/debugger/OfflineWebsite.kt | 65 ++++++++++++++-------- 1 file changed, 41 insertions(+), 24 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/debugger') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/debugger/OfflineWebsite.kt b/app/src/main/kotlin/com/pitchedapps/frost/debugger/OfflineWebsite.kt index 6298f1f9..f5009cc5 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/debugger/OfflineWebsite.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/debugger/OfflineWebsite.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2018 Allan Wang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package com.pitchedapps.frost.debugger import ca.allanwang.kau.logging.KauLoggerExtension @@ -32,21 +48,23 @@ import java.util.zip.ZipOutputStream * * Inspired by Save for Offline */ -class OfflineWebsite(private val url: String, - private val cookie: String = "", - baseUrl: String? = null, - private val html: String? = null, - /** - * Directory that holds all the files - */ - val baseDir: File, - private val userAgent: String = USER_AGENT_BASIC) { +class OfflineWebsite( + private val url: String, + private val cookie: String = "", + baseUrl: String? = null, + private val html: String? = null, + /** + * Directory that holds all the files + */ + val baseDir: File, + private val userAgent: String = USER_AGENT_BASIC +) { /** * Supplied url without the queries */ private val baseUrl = (baseUrl ?: url.substringBefore("?") - .substringBefore(".com")).trim('/') + .substringBefore(".com")).trim('/') private val mainFile = File(baseDir, "index.html") private val assetDir = File(baseDir, "assets") @@ -67,11 +85,11 @@ class OfflineWebsite(private val url: String, private val cssQueue = mutableSetOf() private fun request(url: String) = Request.Builder() - .header("Cookie", cookie) - .header("User-Agent", userAgent) - .url(url) - .get() - .call() + .header("Cookie", cookie) + .header("User-Agent", userAgent) + .url(url) + .get() + .call() private val compositeDisposable = CompositeDisposable() @@ -94,7 +112,6 @@ class OfflineWebsite(private val url: String, return callback(false) } - if (!assetDir.createFreshDir()) { L.e { "Could not create ${assetDir.absolutePath}" } return callback(false) @@ -245,8 +262,10 @@ class OfflineWebsite(private val url: String, } }) - private inline fun String.downloadUrl(fallback: () -> T, - action: (file: File, body: ResponseBody) -> T): T { + private inline fun String.downloadUrl( + fallback: () -> T, + action: (file: File, body: ResponseBody) -> T + ): T { val file = File(assetDir, fileName()) if (!file.createNewFile()) { @@ -289,11 +308,10 @@ class OfflineWebsite(private val url: String, if (mapped != null) return mapped val candidate = substringBefore("?").trim('/') - .substringAfterLast("/").shorten() + .substringAfterLast("/").shorten() val index = atomicInt.getAndIncrement() - var newUrl = "a${index}_$candidate" /** @@ -308,10 +326,10 @@ class OfflineWebsite(private val url: String, } private fun String.shorten() = - if (length <= 10) this else substring(length - 10) + if (length <= 10) this else substring(length - 10) private fun Set.clean(): List = - filter(String::isNotBlank).filter { it.startsWith("http") } + filter(String::isNotBlank).filter { it.startsWith("http") } private fun reset() { cancelled = false @@ -326,5 +344,4 @@ class OfflineWebsite(private val url: String, compositeDisposable.dispose() L.v { "Request cancelled" } } - -} \ No newline at end of file +} -- cgit v1.2.3