mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 18:29:23 +02:00
Book UID (in progress)
This commit is contained in:
parent
201e5bf1b4
commit
41663bd942
6 changed files with 122 additions and 41 deletions
|
@ -29,6 +29,7 @@ import org.geometerplus.zlibrary.core.config.ZLConfig;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.text.view.*;
|
import org.geometerplus.zlibrary.text.view.*;
|
||||||
|
|
||||||
|
import org.geometerplus.fbreader.book.*;
|
||||||
import org.geometerplus.fbreader.fbreader.*;
|
import org.geometerplus.fbreader.fbreader.*;
|
||||||
|
|
||||||
public class ApiServerImplementation extends ApiInterface.Stub implements Api, ApiMethods {
|
public class ApiServerImplementation extends ApiInterface.Stub implements Api, ApiMethods {
|
||||||
|
@ -310,7 +311,8 @@ public class ApiServerImplementation extends ApiInterface.Stub implements Api, A
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBookHash() {
|
public String getBookHash() {
|
||||||
return getReader().Model.Book.getContentHashCode();
|
final UID uid = BookUtil.createSHA256Uid(getReader().Model.Book.File);
|
||||||
|
return uid != null ? uid.Id : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBookUniqueId() {
|
public String getBookUniqueId() {
|
||||||
|
|
|
@ -458,7 +458,7 @@ final class SQLiteBooksDatabase extends BooksDatabase {
|
||||||
cursor.close();
|
cursor.close();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
ArrayList<Tag> list = new ArrayList<Tag>();
|
final ArrayList<Tag> list = new ArrayList<Tag>();
|
||||||
do {
|
do {
|
||||||
list.add(getTagById(cursor.getLong(0)));
|
list.add(getTagById(cursor.getLong(0)));
|
||||||
} while (cursor.moveToNext());
|
} while (cursor.moveToNext());
|
||||||
|
@ -466,6 +466,32 @@ final class SQLiteBooksDatabase extends BooksDatabase {
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SQLiteStatement myInsertBookUidStatement;
|
||||||
|
protected void saveBookUid(long bookId, UID uid) {
|
||||||
|
if (myInsertBookUidStatement == null) {
|
||||||
|
myInsertBookUidStatement = myDatabase.compileStatement(
|
||||||
|
"INSERT OR REPLACE INTO BookUid (book_id,type,uid) VALUES (?,?,?)"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized (myInsertBookUidStatement) {
|
||||||
|
myInsertBookUidStatement.bindLong(1, bookId);
|
||||||
|
myInsertBookUidStatement.bindString(2, uid.Type);
|
||||||
|
myInsertBookUidStatement.bindString(2, uid.Id);
|
||||||
|
myInsertBookAuthorStatement.execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected List<UID> listUids(long bookId) {
|
||||||
|
final ArrayList<UID> list = new ArrayList<UID>();
|
||||||
|
final Cursor cursor = myDatabase.rawQuery("SELECT type,uid FROM BookUid WHERE book_id = ?", new String[] { "" + bookId });
|
||||||
|
if (cursor.moveToNext()) {
|
||||||
|
list.add(new UID(cursor.getString(0), cursor.getString(1)));
|
||||||
|
}
|
||||||
|
cursor.close();
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
private SQLiteStatement myGetSeriesIdStatement;
|
private SQLiteStatement myGetSeriesIdStatement;
|
||||||
private SQLiteStatement myInsertSeriesStatement;
|
private SQLiteStatement myInsertSeriesStatement;
|
||||||
private SQLiteStatement myInsertBookSeriesStatement;
|
private SQLiteStatement myInsertBookSeriesStatement;
|
||||||
|
|
|
@ -22,10 +22,6 @@ package org.geometerplus.fbreader.book;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.security.MessageDigest;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.filesystem.*;
|
import org.geometerplus.zlibrary.core.filesystem.*;
|
||||||
import org.geometerplus.zlibrary.core.image.ZLImage;
|
import org.geometerplus.zlibrary.core.image.ZLImage;
|
||||||
|
@ -51,6 +47,7 @@ public class Book extends TitledEntity {
|
||||||
private volatile List<Author> myAuthors;
|
private volatile List<Author> myAuthors;
|
||||||
private volatile List<Tag> myTags;
|
private volatile List<Tag> myTags;
|
||||||
private volatile SeriesInfo mySeriesInfo;
|
private volatile SeriesInfo mySeriesInfo;
|
||||||
|
private volatile List<UID> myUids;
|
||||||
|
|
||||||
private volatile boolean myIsSaved;
|
private volatile boolean myIsSaved;
|
||||||
|
|
||||||
|
@ -140,6 +137,7 @@ public class Book extends TitledEntity {
|
||||||
myAuthors = database.listAuthors(myId);
|
myAuthors = database.listAuthors(myId);
|
||||||
myTags = database.listTags(myId);
|
myTags = database.listTags(myId);
|
||||||
mySeriesInfo = database.getSeriesInfo(myId);
|
mySeriesInfo = database.getSeriesInfo(myId);
|
||||||
|
myUids = database.listUids(myId);
|
||||||
myIsSaved = true;
|
myIsSaved = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -400,41 +398,6 @@ public class Book extends TitledEntity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getContentHashCode() {
|
|
||||||
InputStream stream = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
final MessageDigest hash = MessageDigest.getInstance("SHA-256");
|
|
||||||
stream = File.getInputStream();
|
|
||||||
|
|
||||||
final byte[] buffer = new byte[2048];
|
|
||||||
while (true) {
|
|
||||||
final int nread = stream.read(buffer);
|
|
||||||
if (nread == -1) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
hash.update(buffer, 0, nread);
|
|
||||||
}
|
|
||||||
|
|
||||||
final Formatter f = new Formatter();
|
|
||||||
for (byte b : hash.digest()) {
|
|
||||||
f.format("%02X", b & 0xFF);
|
|
||||||
}
|
|
||||||
return f.toString();
|
|
||||||
} catch (IOException e) {
|
|
||||||
return null;
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
return null;
|
|
||||||
} finally {
|
|
||||||
if (stream != null) {
|
|
||||||
try {
|
|
||||||
stream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
synchronized ZLImage getCover() {
|
synchronized ZLImage getCover() {
|
||||||
if (myCover == NULL_IMAGE) {
|
if (myCover == NULL_IMAGE) {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -19,6 +19,11 @@
|
||||||
|
|
||||||
package org.geometerplus.fbreader.book;
|
package org.geometerplus.fbreader.book;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Formatter;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.filesystem.*;
|
import org.geometerplus.zlibrary.core.filesystem.*;
|
||||||
|
@ -73,4 +78,40 @@ public abstract class BookUtil {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static UID createSHA256Uid(ZLFile file) {
|
||||||
|
InputStream stream = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
final MessageDigest hash = MessageDigest.getInstance("SHA-256");
|
||||||
|
stream = file.getInputStream();
|
||||||
|
|
||||||
|
final byte[] buffer = new byte[2048];
|
||||||
|
while (true) {
|
||||||
|
final int nread = stream.read(buffer);
|
||||||
|
if (nread == -1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
hash.update(buffer, 0, nread);
|
||||||
|
}
|
||||||
|
|
||||||
|
final Formatter f = new Formatter();
|
||||||
|
for (byte b : hash.digest()) {
|
||||||
|
f.format("%02X", b & 0xFF);
|
||||||
|
}
|
||||||
|
return new UID("SHA256", f.toString());
|
||||||
|
} catch (IOException e) {
|
||||||
|
return null;
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
if (stream != null) {
|
||||||
|
try {
|
||||||
|
stream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,6 +54,7 @@ public abstract class BooksDatabase {
|
||||||
protected abstract List<Author> listAuthors(long bookId);
|
protected abstract List<Author> listAuthors(long bookId);
|
||||||
protected abstract List<Tag> listTags(long bookId);
|
protected abstract List<Tag> listTags(long bookId);
|
||||||
protected abstract SeriesInfo getSeriesInfo(long bookId);
|
protected abstract SeriesInfo getSeriesInfo(long bookId);
|
||||||
|
protected abstract List<UID> listUids(long bookId);
|
||||||
|
|
||||||
protected abstract void updateBookInfo(long bookId, long fileId, String encoding, String language, String title);
|
protected abstract void updateBookInfo(long bookId, long fileId, String encoding, String language, String title);
|
||||||
protected abstract long insertBookInfo(ZLFile file, String encoding, String language, String title);
|
protected abstract long insertBookInfo(ZLFile file, String encoding, String language, String title);
|
||||||
|
@ -62,6 +63,7 @@ public abstract class BooksDatabase {
|
||||||
protected abstract void deleteAllBookTags(long bookId);
|
protected abstract void deleteAllBookTags(long bookId);
|
||||||
protected abstract void saveBookTagInfo(long bookId, Tag tag);
|
protected abstract void saveBookTagInfo(long bookId, Tag tag);
|
||||||
protected abstract void saveBookSeriesInfo(long bookId, SeriesInfo seriesInfo);
|
protected abstract void saveBookSeriesInfo(long bookId, SeriesInfo seriesInfo);
|
||||||
|
protected abstract void saveBookUid(long bookId, UID uid);
|
||||||
|
|
||||||
protected FileInfo createFileInfo(long id, String name, FileInfo parent) {
|
protected FileInfo createFileInfo(long id, String name, FileInfo parent) {
|
||||||
return new FileInfo(name, parent, id);
|
return new FileInfo(name, parent, id);
|
||||||
|
|
47
src/org/geometerplus/fbreader/book/UID.java
Normal file
47
src/org/geometerplus/fbreader/book/UID.java
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2007-2013 Geometer Plus <contact@geometerplus.com>
|
||||||
|
*
|
||||||
|
* 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 2 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, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||||
|
* 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.geometerplus.fbreader.book;
|
||||||
|
|
||||||
|
public class UID {
|
||||||
|
public final String Type;
|
||||||
|
public final String Id;
|
||||||
|
|
||||||
|
public UID(String type, String id) {
|
||||||
|
Type = type;
|
||||||
|
Id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!(o instanceof UID)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
final UID u = (UID)o;
|
||||||
|
return Type.equals(u.Type) && Id.equals(u.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Type.hashCode() + Id.hashCode();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue