Compare commits

..
7 Commits
Author SHA1 Message Date
seven332 ba6d41b91f DEPRECATED 2019-08-09 12:50:16 +08:00
seven332 6a9e0198a2 Add cookie to torrent request 2019-08-07 11:05:38 +08:00
seven332 92f716170c Fix plain txt checking 2019-08-07 09:55:21 +08:00
seven332 d72f686d5a Fix data exporting 2019-08-01 17:02:36 +08:00
seven332 28eade9451 Fix check plain txt 2019-07-31 20:06:02 +08:00
seven332 7b95bc804a Check plain text after downloading 2019-07-31 19:41:51 +08:00
seven332 47c9d48787 Close #659, fix parsing gallery parent 2019-07-29 14:04:27 +08:00
15 changed files with 262 additions and 88 deletions

No files matched your search

+2
View File
@@ -1,3 +1,5 @@
# DEPRECATED
[![Telegram](https://img.shields.io/badge/chat-Telegram-blue.svg)](https://t.me/ehviewer)
# EhViewer
+57 -22
View File
@@ -44,10 +44,11 @@ import com.hippo.ehviewer.dao.QuickSearchDao;
import com.hippo.ehviewer.download.DownloadManager;
import com.hippo.util.ExceptionUtils;
import com.hippo.util.SqlUtils;
import com.hippo.yorozuya.FileUtils;
import com.hippo.yorozuya.IOUtils;
import com.hippo.yorozuya.ObjectUtils;
import com.hippo.yorozuya.collect.SparseJLArray;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.query.CloseableListIterator;
import de.greenrobot.dao.query.LazyList;
import java.io.File;
import java.io.FileInputStream;
@@ -645,30 +646,64 @@ public class EhDB {
sDaoSession.getFilterDao().update(filter);
}
public static synchronized boolean exportDB(Context context, File file) {
File dbFile = context.getDatabasePath("eh.db");
if (null == dbFile || !dbFile.isFile()) {
return false;
}
if (null == file || !FileUtils.ensureFile(file)) {
return false;
}
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(dbFile);
os = new FileOutputStream(file);
IOUtils.copy(is, os);
return true;
private static <T> boolean copyDao(AbstractDao<T, ?> from, AbstractDao<T, ?> to) {
try (CloseableListIterator<T> iterator = from.queryBuilder().listIterator()) {
while (iterator.hasNext()) {
to.insert(iterator.next());
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static synchronized boolean exportDB(Context context, File file) {
final String ehExportName = "eh.export.db";
// Delete old export db
context.deleteDatabase(ehExportName);
DBOpenHelper helper = new DBOpenHelper(context.getApplicationContext(), ehExportName, null);
try {
// Copy data to a export db
try (SQLiteDatabase db = helper.getWritableDatabase()) {
DaoMaster daoMaster = new DaoMaster(db);
DaoSession exportSession = daoMaster.newSession();
if (!copyDao(sDaoSession.getDownloadsDao(), exportSession.getDownloadsDao())) return false;
if (!copyDao(sDaoSession.getDownloadLabelDao(), exportSession.getDownloadLabelDao())) return false;
if (!copyDao(sDaoSession.getDownloadDirnameDao(), exportSession.getDownloadDirnameDao())) return false;
if (!copyDao(sDaoSession.getHistoryDao(), exportSession.getHistoryDao())) return false;
if (!copyDao(sDaoSession.getQuickSearchDao(), exportSession.getQuickSearchDao())) return false;
if (!copyDao(sDaoSession.getLocalFavoritesDao(), exportSession.getLocalFavoritesDao())) return false;
if (!copyDao(sDaoSession.getBookmarksBao(), exportSession.getBookmarksBao())) return false;
if (!copyDao(sDaoSession.getFilterDao(), exportSession.getFilterDao())) return false;
}
// Copy export db to data dir
File dbFile = context.getDatabasePath(ehExportName);
if (dbFile == null || !dbFile.isFile()) {
return false;
}
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(dbFile);
os = new FileOutputStream(file);
IOUtils.copy(is, os);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
// Delete failed file
file.delete();
return false;
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
context.deleteDatabase(ehExportName);
}
// Delete failed file
file.delete();
return false;
}
/**
@@ -272,7 +272,10 @@ public class GalleryDetailParser {
if (key.startsWith("Posted")) {
gd.posted = value;
} else if (key.startsWith("Parent")) {
gd.parent = value;
Element a = es.get(1).children().first();
if (a != null) {
gd.parent = a.attr("href");
}
} else if (key.startsWith("Visible")) {
gd.visible = value;
} else if (key.startsWith("Language")) {
@@ -0,0 +1,78 @@
/*
* Copyright 2019 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.preference;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.hippo.ehviewer.AppConfig;
import com.hippo.ehviewer.EhDB;
import com.hippo.ehviewer.GetText;
import com.hippo.ehviewer.R;
import com.hippo.util.ReadableTime;
import java.io.File;
public class ExportDataPreference extends TaskPreference {
public ExportDataPreference(Context context) {
super(context);
}
public ExportDataPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExportDataPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@NonNull
@Override
protected Task onCreateTask() {
return new ExportDataTask(getContext());
}
private static class ExportDataTask extends Task {
public ExportDataTask(@NonNull Context context) {
super(context);
}
@Override
protected Object doInBackground(Void... voids) {
File dir = AppConfig.getExternalDataDir();
if (dir != null) {
File file = new File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".db");
if (EhDB.exportDB(getApplication(), file)) {
return file;
}
}
return null;
}
@Override
protected void onPostExecute(Object o) {
Toast.makeText(getApplication(),
(o instanceof File)
? GetText.getString(R.string.settings_advanced_export_data_to, ((File) o).getPath())
: GetText.getString(R.string.settings_advanced_export_data_failed),
Toast.LENGTH_SHORT).show();
super.onPostExecute(o);
}
}
}
@@ -166,6 +166,10 @@ public abstract class TaskPreference extends DialogPreference {
mApplication = (EhApplication) context.getApplicationContext();
}
public EhApplication getApplication() {
return mApplication;
}
@Nullable
public TaskPreference getPreference() {
return mPreference;
@@ -61,6 +61,7 @@ import com.hippo.yorozuya.Utilities;
import com.hippo.yorozuya.collect.SparseJLArray;
import com.hippo.yorozuya.thread.PriorityThread;
import com.hippo.yorozuya.thread.PriorityThreadFactory;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -1172,7 +1173,6 @@ public final class SpiderQueen implements Runnable {
}
// Download image
OutputStreamPipe pipe = null;
InputStream is = null;
try {
if (DEBUG_LOG) {
@@ -1208,49 +1208,93 @@ public final class SpiderQueen implements Runnable {
extension = GalleryProvider2.SUPPORT_IMAGE_EXTENSIONS[0];
}
// Get out put pipe
pipe = mSpiderDen.openOutputStreamPipe(index, extension);
if (null == pipe) {
// Can't get pipe
error = GetText.getString(R.string.error_write_failed);
response.close();
break;
}
long contentLength = responseBody.contentLength();
is = responseBody.byteStream();
pipe.obtain();
OutputStream os = pipe.open();
final byte data[] = new byte[1024 * 4];
long receivedSize = 0;
while (!Thread.currentThread().isInterrupted()) {
int bytesRead = is.read(data);
if (bytesRead == -1) {
OutputStreamPipe osPipe = null;
try {
// Get out put pipe
osPipe = mSpiderDen.openOutputStreamPipe(index, extension);
if (osPipe == null) {
// Can't get pipe
error = GetText.getString(R.string.error_write_failed);
response.close();
break;
}
os.write(data, 0, bytesRead);
receivedSize += bytesRead;
// Update page percent
if (contentLength > 0) {
mPagePercentMap.put(index, (float) receivedSize / contentLength);
}
// Notify listener
notifyPageDownload(index, contentLength, receivedSize, bytesRead);
}
os.flush();
// check download size
if (contentLength >= 0) {
if (receivedSize < contentLength) {
Log.e(TAG, "Can't download all of image data");
error = "Incomplete";
long contentLength = responseBody.contentLength();
is = responseBody.byteStream();
osPipe.obtain();
OutputStream os = osPipe.open();
final byte[] data = new byte[1024 * 4];
long receivedSize = 0;
while (!Thread.currentThread().isInterrupted()) {
int bytesRead = is.read(data);
if (bytesRead == -1) {
response.close();
break;
}
os.write(data, 0, bytesRead);
receivedSize += bytesRead;
// Update page percent
if (contentLength > 0) {
mPagePercentMap.put(index, (float) receivedSize / contentLength);
}
// Notify listener
notifyPageDownload(index, contentLength, receivedSize, bytesRead);
}
os.flush();
// check download size
if (contentLength >= 0) {
if (receivedSize < contentLength) {
Log.e(TAG, "Can't download all of image data");
error = "Incomplete";
forceHtml = true;
continue;
} else if (receivedSize > contentLength) {
Log.w(TAG, "Received data is more than contentLength");
}
}
} finally {
if (osPipe != null) {
osPipe.close();
osPipe.release();
}
}
InputStreamPipe isPipe = null;
try {
// Get InputStreamPipe
isPipe = mSpiderDen.openInputStreamPipe(index);
if (isPipe == null) {
// Can't get pipe
error = GetText.getString(R.string.error_reading_failed);
break;
}
// Check plain txt
isPipe.obtain();
InputStream inputStream = new BufferedInputStream(isPipe.open());
boolean isPlainTxt = true;
for (;;) {
int b = inputStream.read();
if (b == -1) {
break;
}
if (b > 126) {
isPlainTxt = false;
break;
}
}
if (isPlainTxt) {
error = GetText.getString(R.string.error_reading_failed);
forceHtml = true;
continue;
} else if (receivedSize > contentLength) {
Log.w(TAG, "Received data is more than contentLength");
}
} finally {
if (isPipe != null) {
isPipe.close();
isPipe.release();
}
}
@@ -1274,10 +1318,6 @@ public final class SpiderQueen implements Runnable {
forceHtml = true;
} finally {
IOUtils.closeQuietly(is);
if (null != pipe) {
pipe.close();
pipe.release();
}
if (DEBUG_LOG) {
Log.d(TAG, "End download image " + index);
@@ -40,7 +40,6 @@ public class AdvancedFragment extends PreferenceFragment
private static final String KEY_DUMP_LOGCAT = "dump_logcat";
private static final String KEY_CLEAR_MEMORY_CACHE = "clear_memory_cache";
private static final String KEY_APP_LANGUAGE = "app_language";
private static final String KEY_EXPORT_DATA = "export_data";
private static final String KEY_IMPORT_DATA = "import_data";
@Override
@@ -51,12 +50,10 @@ public class AdvancedFragment extends PreferenceFragment
Preference dumpLogcat = findPreference(KEY_DUMP_LOGCAT);
Preference clearMemoryCache = findPreference(KEY_CLEAR_MEMORY_CACHE);
Preference appLanguage = findPreference(KEY_APP_LANGUAGE);
Preference exportData = findPreference(KEY_EXPORT_DATA);
Preference importData = findPreference(KEY_IMPORT_DATA);
dumpLogcat.setOnPreferenceClickListener(this);
clearMemoryCache.setOnPreferenceClickListener(this);
exportData.setOnPreferenceClickListener(this);
importData.setOnPreferenceClickListener(this);
appLanguage.setOnPreferenceChangeListener(this);
@@ -88,18 +85,6 @@ public class AdvancedFragment extends PreferenceFragment
} else if (KEY_CLEAR_MEMORY_CACHE.equals(key)) {
((EhApplication) getActivity().getApplication()).clearMemoryCache();
Runtime.getRuntime().gc();
} else if (KEY_EXPORT_DATA.equals(key)) {
File dir = AppConfig.getExternalDataDir();
if (dir != null) {
File file = new File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".db");
if (EhDB.exportDB(getActivity(), file)) {
Toast.makeText(getActivity(),
getString(R.string.settings_advanced_export_data_to, file.getPath()), Toast.LENGTH_SHORT).show();
return true;
}
}
Toast.makeText(getActivity(),R.string.settings_advanced_export_data_failed, Toast.LENGTH_SHORT).show();
return true;
} else if (KEY_IMPORT_DATA.equals(key)) {
importData(getActivity());
getActivity().setResult(Activity.RESULT_OK);
@@ -116,6 +116,7 @@ import java.io.OutputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import okhttp3.HttpUrl;
public class GalleryDetailScene extends BaseScene implements View.OnClickListener,
com.hippo.ehviewer.download.DownloadManager.DownloadInfoListener,
@@ -1875,6 +1876,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene
FileUtils.sanitizeFilename(name + ".torrent"));
r.allowScanningByMediaScanner();
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
r.addRequestHeader("Cookie", EhApplication.getEhCookieStore(context).getCookieHeader(HttpUrl.get(url)));
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
if (dm != null) {
try {
@@ -36,6 +36,7 @@ import com.hippo.easyrecyclerview.EasyRecyclerView;
import com.hippo.easyrecyclerview.LinearDividerItemDecoration;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.Settings;
import com.hippo.ehviewer.UrlOpener;
import com.hippo.ehviewer.client.EhUrl;
import com.hippo.ehviewer.client.EhUtils;
import com.hippo.ehviewer.client.data.GalleryDetail;
@@ -51,6 +52,9 @@ public final class GalleryInfoScene extends ToolbarScene implements EasyRecycler
public static final String KEY_KEYS = "keys";
public static final String KEY_VALUES = "values";
private static final int INDEX_URL = 3;
private static final int INDEX_PARENT = 10;
/*---------------
Whole life cycle
---------------*/
@@ -199,15 +203,19 @@ public final class GalleryInfoScene extends ToolbarScene implements EasyRecycler
public boolean onItemClick(EasyRecyclerView parent, View view, int position, long id) {
Context context = getContext2();
if (null != context && 0 != position && null != mValues) {
ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
cmb.setPrimaryClip(ClipData.newPlainText(null, mValues.get(position)));
if (position == INDEX_PARENT) {
UrlOpener.openUrl(context, mValues.get(position), true);
} else {
ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
cmb.setPrimaryClip(ClipData.newPlainText(null, mValues.get(position)));
if (position == 3) {
// Save it to avoid detect the gallery
Settings.putClipboardTextHashCode(mValues.get(position).hashCode());
if (position == INDEX_URL) {
// Save it to avoid detect the gallery
Settings.putClipboardTextHashCode(mValues.get(position).hashCode());
}
showTip(R.string.copied_to_clipboard, LENGTH_SHORT);
}
showTip(R.string.copied_to_clipboard, LENGTH_SHORT);
return true;
} else {
return false;
@@ -85,6 +85,19 @@ public class CookieRepository implements CookieJar {
}
}
public String getCookieHeader(HttpUrl url) {
List<Cookie> cookies = getCookies(url);
StringBuilder cookieHeader = new StringBuilder();
for (int i = 0, size = cookies.size(); i < size; i++) {
if (i > 0) {
cookieHeader.append("; ");
}
Cookie cookie = cookies.get(i);
cookieHeader.append(cookie.name()).append('=').append(cookie.value());
}
return cookieHeader.toString();
}
public synchronized List<Cookie> getCookies(HttpUrl url) {
List<Cookie> accepted = new ArrayList<>();
List<Cookie> expired = new ArrayList<>();
@@ -92,6 +92,7 @@
<string name="error_invalid_number">非法数字</string>
<string name="error_please_login_first">请先登陆</string>
<string name="error_empty_html">页面内容为空</string>
<string name="error_cant_get_image">无法获取图片</string>
<string name="kokomade_tip">设置->Eh->画廊站点->e-hentai</string>
@@ -92,6 +92,7 @@
<string name="error_invalid_number">非法數字</string>
<string name="error_please_login_first">請先登陸</string>
<string name="error_empty_html">頁面內容為空</string>
<string name="error_cant_get_image">無法獲取圖片</string>
<string name="kokomade_tip">設置->Eh->畫廊站點->e-hentai</string>
@@ -92,6 +92,7 @@
<string name="error_invalid_number">無效的數字</string>
<string name="error_please_login_first">請先登陸</string>
<string name="error_empty_html">頁面內容為空</string>
<string name="error_cant_get_image">無法獲取圖片</string>
<string name="kokomade_tip">設定->Eh->圖庫站台->e-hentai</string>
+1
View File
@@ -134,6 +134,7 @@
<string name="error_invalid_number">Invalid number</string>
<string name="error_please_login_first">Please login first</string>
<string name="error_empty_html">Empty page</string>
<string name="error_cant_get_image">Can\'t get image</string>
<string name="kokomade_tip">Settings->Eh->Gallery Site->e-hentai</string>
+1 -1
View File
@@ -80,7 +80,7 @@
android:summary="@string/settings_advanced_custom_hosts_summary"
app:activity="com.hippo.ehviewer.ui.HostsActivity"/>
<Preference
<com.hippo.ehviewer.preference.ExportDataPreference
android:key="export_data"
android:title="@string/settings_advanced_export_data"
android:summary="@string/settings_advanced_export_data_summary"/>