Skip to content

Commit

Permalink
format code.
Browse files Browse the repository at this point in the history
  • Loading branch information
shentar committed Jul 10, 2016
1 parent 8a70319 commit 5a77e60
Show file tree
Hide file tree
Showing 25 changed files with 372 additions and 197 deletions.
36 changes: 23 additions & 13 deletions photoweb/src/com/backend/BaseSqliteStore.java
Expand Up @@ -21,7 +21,8 @@

public class BaseSqliteStore
{
private static final Logger logger = LoggerFactory.getLogger(BaseSqliteStore.class);
private static final Logger logger = LoggerFactory
.getLogger(BaseSqliteStore.class);

private Connection conn = SqliteConnManger.getInstance().getConn();

Expand All @@ -30,7 +31,8 @@ public class BaseSqliteStore
private static BaseSqliteStore instance = new BaseSqliteStore();

// 对于树莓派等系统,最多只能2个线程同时计算缩略图。
public static final ExecutorService threadPool = Executors.newFixedThreadPool(2);
public static final ExecutorService threadPool = Executors
.newFixedThreadPool(2);

private BaseSqliteStore()
{
Expand Down Expand Up @@ -79,7 +81,7 @@ public FileInfo getOneFileByHashID(String id)
{
lock.readLock().unlock();
}

return null;
}

Expand Down Expand Up @@ -112,7 +114,8 @@ private void insertOneRecord(File f, String sha256)
}
fi.setHash256(sha256);
lock.writeLock().lock();
prep = conn.prepareStatement("insert into files values(?,?,?,?,?,?,?);");
prep = conn.prepareStatement(
"insert into files values(?,?,?,?,?,?,?);");
prep.setString(1, fi.getPath());
prep.setString(2, fi.getHash256());
prep.setLong(3, fi.getSize());
Expand Down Expand Up @@ -167,14 +170,16 @@ public boolean checkIfAlreadyExist(File f)
{
FileInfo oldfi = getFileInfoFromTable(res);
if (oldfi.getSize() == f.length() && oldfi.getcTime() != null
&& oldfi.getcTime().getTime() == FileTools.getFileCreateTime(f))
&& oldfi.getcTime().getTime() == FileTools
.getFileCreateTime(f))
{
checkPhotoTime(oldfi);
return true;
}
else
{
logger.warn("the file was changed, rebuild the record: " + oldfi);
logger.warn("the file was changed, rebuild the record: "
+ oldfi);
deleteOneRecord(oldfi);
return false;
}
Expand Down Expand Up @@ -239,7 +244,8 @@ private void checkPhotoTime(FileInfo oldfi) throws IOException
{
boolean bneedupdate = false;
if (oldfi.getPhotoTime() == null || oldfi.getcTime() == null
|| oldfi.getPhotoTime().getTime() > System.currentTimeMillis())
|| oldfi.getPhotoTime().getTime() > System
.currentTimeMillis())
{
oldfi = ReadEXIF.genAllInfos(oldfi.getPath(), true);
bneedupdate = true;
Expand All @@ -260,8 +266,8 @@ private void updatePhotoInfo(FileInfo fi)
try
{
lock.writeLock().lock();
PreparedStatement prep = conn
.prepareStatement("update files set phototime=?,width=?,height=? where path=?;");
PreparedStatement prep = conn.prepareStatement(
"update files set phototime=?,width=?,height=? where path=?;");
prep.setDate(1, fi.getPhotoTime());
prep.setLong(2, fi.getWidth());
prep.setLong(3, fi.getHeight());
Expand Down Expand Up @@ -302,7 +308,8 @@ public void scanAllRecords(List<String> excludeDirs)
}
else
{
if (ThumbnailManager.checkTheThumbnailExist(fi.getHash256()))
if (ThumbnailManager
.checkTheThumbnailExist(fi.getHash256()))
{
PerformanceStatistics.getInstance().addOneFile(false);
continue;
Expand All @@ -314,7 +321,8 @@ public void scanAllRecords(List<String> excludeDirs)
}
}
prep.close();
PerformanceStatistics.getInstance().printPerformanceLog(System.currentTimeMillis());
PerformanceStatistics.getInstance()
.printPerformanceLog(System.currentTimeMillis());
logger.warn("end checking all records in the files table.");
}
catch (Exception e)
Expand All @@ -330,7 +338,8 @@ private void submitAnThumbnailTask(final FileInfo fi)
isdone = GloableLockBaseOnString.getInstance().tryToDo(fi.getHash256());
if (!isdone)
{
logger.info("the task of pic id [{}] is already being done.", fi.getHash256());
logger.info("the task of pic id [{}] is already being done.",
fi.getHash256());
return;
}

Expand Down Expand Up @@ -390,7 +399,8 @@ public void deleteRecordsInDirs(String dir)
try
{
lock.writeLock().lock();
prep = conn.prepareStatement("delete from files where path like ?;");
prep = conn
.prepareStatement("delete from files where path like ?;");
prep.setString(1, dir + "%");

prep.execute();
Expand Down
3 changes: 2 additions & 1 deletion photoweb/src/com/backend/DateRecords.java
Expand Up @@ -53,7 +53,8 @@ public int hashCode()
final int prime = 31;
int result = 1;
result = prime * result + ((datestr == null) ? 0 : datestr.hashCode());
result = prime * result + ((datetype == null) ? 0 : datetype.hashCode());
result = prime * result
+ ((datetype == null) ? 0 : datetype.hashCode());
return result;
}

Expand Down
15 changes: 10 additions & 5 deletions photoweb/src/com/backend/DateTableDao.java
Expand Up @@ -16,7 +16,8 @@

public class DateTableDao
{
private static final Logger logger = LoggerFactory.getLogger(DateTableDao.class);
private static final Logger logger = LoggerFactory
.getLogger(DateTableDao.class);

private Connection conn = SqliteConnManger.getInstance().getConn();

Expand Down Expand Up @@ -58,7 +59,8 @@ public TreeMap<String, TreeMap<String, TreeMap<String, DateRecords>>> getAllDate
String month = date.substring(4, 6);
String year = date.substring(0, 4);

TreeMap<String, TreeMap<String, DateRecords>> myear = allrecords.get(year);
TreeMap<String, TreeMap<String, DateRecords>> myear = allrecords
.get(year);
if (myear == null)
{
myear = new TreeMap<String, TreeMap<String, DateRecords>>();
Expand Down Expand Up @@ -106,7 +108,8 @@ public DateRecords getOneRecordsByDay(String day)
ResultSet res = null;
try
{
prep = conn.prepareStatement("select * from daterecords where datestr=?;");
prep = conn.prepareStatement(
"select * from daterecords where datestr=?;");
prep.setString(1, day);
res = prep.executeQuery();

Expand Down Expand Up @@ -138,7 +141,8 @@ public void refreshDate()
{
logger.warn("refresh all date record.");

Map<String, DateRecords> dst = UniqPhotosStore.getInstance().genAllDateRecords();
Map<String, DateRecords> dst = UniqPhotosStore.getInstance()
.genAllDateRecords();
if (dst == null || dst.isEmpty())
{
logger.warn("there is no pic.");
Expand All @@ -153,7 +157,8 @@ public void refreshDate()
prep.execute();
prep.close();

prep = conn.prepareStatement("insert into daterecords values(?,?,?);");
prep = conn
.prepareStatement("insert into daterecords values(?,?,?);");
for (Entry<String, DateRecords> dr : dst.entrySet())
{
prep.setString(1, dr.getKey());
Expand Down
15 changes: 10 additions & 5 deletions photoweb/src/com/backend/ExifCreator.java
Expand Up @@ -29,7 +29,8 @@
public class ExifCreator
{

private static final Logger logger = LoggerFactory.getLogger(ExifCreator.class);
private static final Logger logger = LoggerFactory
.getLogger(ExifCreator.class);

public static void addExifDate(String[] args) throws Exception
{
Expand Down Expand Up @@ -88,7 +89,8 @@ public static void addExifDate(String[] args) throws Exception
if (!(imageInfo instanceof Exif))
{
logger.info("Adding a Dummy Exif Header");
llj.addAppx(LLJTran.dummyExifHeader, 0, LLJTran.dummyExifHeader.length, true);
llj.addAppx(LLJTran.dummyExifHeader, 0,
LLJTran.dummyExifHeader.length, true);
imageInfo = llj.getImageInfo(); // This would have changed

Exif exif = (Exif) imageInfo;
Expand Down Expand Up @@ -118,15 +120,17 @@ public static void addExifDate(String[] args) throws Exception
}

// 4. Set the new Thumbnail
if (llj.setThumbnail(newThumbnail, 0, newThumbnail.length, ImageResources.EXT_JPG))
if (llj.setThumbnail(newThumbnail, 0, newThumbnail.length,
ImageResources.EXT_JPG))
logger.info("Successfully Set New Thumbnail");
else
logger.info("Error Setting New Thumbnail");

// 5. Transfer the image from inputFile to outputFile replacing the new
// Exif with the Thumbnail so that outputFile has a Thumbnail.
fip = new BufferedInputStream(new FileInputStream(args[0]));
OutputStream out = new BufferedOutputStream(new FileOutputStream(args[1]));
OutputStream out = new BufferedOutputStream(
new FileOutputStream(args[1]));
// Replace the new Exif Header in llj while copying the image from fip
// to out
llj.xferInfo(fip, out, LLJTran.REPLACE, LLJTran.RETAIN);
Expand Down Expand Up @@ -168,7 +172,8 @@ private static byte[] getThumbnailImage(InputStream ip) throws IOException
factor = factor1;
AffineTransform tx = new AffineTransform();
tx.scale(factor, factor);
AffineTransformOp affineOp = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
AffineTransformOp affineOp = new AffineTransformOp(tx,
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = affineOp.filter(image, null);

// Write Out the Scaled Image to a ByteArrayOutputStream and return the
Expand Down
3 changes: 2 additions & 1 deletion photoweb/src/com/backend/FileInfo.java
Expand Up @@ -50,7 +50,8 @@ public void setPhotoTime(Date photoTime)

public String toString()
{
return "[path: " + path + "; hahstr: " + hash256 + "; phototime: " + photoTime + "]";
return "[path: " + path + "; hahstr: " + hash256 + "; phototime: "
+ photoTime + "]";
}

public long getHeight()
Expand Down
9 changes: 6 additions & 3 deletions photoweb/src/com/backend/FileSHA256Caculater.java
Expand Up @@ -14,14 +14,16 @@ public class FileSHA256Caculater
{
public static final int BUFFERED_SIZE = 1024 * 128;

public static String calFileSha256(File f) throws NoSuchAlgorithmException, IOException
public static String calFileSha256(File f)
throws NoSuchAlgorithmException, IOException
{
if (f == null || !f.exists() || f.isDirectory())
{
return null;
}

MessageDigest md = MessageDigest.getInstance(AppConfig.getInstance().getHashAlog());
MessageDigest md = MessageDigest
.getInstance(AppConfig.getInstance().getHashAlog());
InputStream in = null;
try
{
Expand All @@ -34,7 +36,8 @@ public static String calFileSha256(File f) throws NoSuchAlgorithmException, IOEx
boolean bcomplete = false;
while (offset < BUFFERED_SIZE)
{
int readlen = in.read(buffer, offset, BUFFERED_SIZE - offset);
int readlen = in.read(buffer, offset,
BUFFERED_SIZE - offset);
if (readlen < 0)
{
bcomplete = true;
Expand Down
33 changes: 22 additions & 11 deletions photoweb/src/com/backend/FileTools.java
Expand Up @@ -30,13 +30,16 @@

public class FileTools
{
private static final Logger logger = LoggerFactory.getLogger(FileTools.class);
public static boolean usesqlite = "true".equals(System.getProperty("usesqlite", "true")) ? true : false;
private static final Logger logger = LoggerFactory
.getLogger(FileTools.class);
public static boolean usesqlite = "true"
.equals(System.getProperty("usesqlite", "true")) ? true : false;
public static long lastScanTime = 0;
public static final ExecutorService threadPool = Executors
.newFixedThreadPool(AppConfig.getInstance().getThreadCount());

public static void readShortFileContent(byte[] buffer, File f) throws FileNotFoundException, IOException
public static void readShortFileContent(byte[] buffer, File f)
throws FileNotFoundException, IOException
{
int maxLen = buffer.length;
FileInputStream fin = null;
Expand Down Expand Up @@ -70,14 +73,17 @@ public static void readShortFileContent(byte[] buffer, File f) throws FileNotFou
}
}

public static boolean checkFileDeleted(final FileInfo fi, List<String> excludeDirs) throws IOException
public static boolean checkFileDeleted(final FileInfo fi,
List<String> excludeDirs) throws IOException
{
if (fi != null)
{
File f = new File(fi.getPath());
if (f.isFile() && f.length() > AppConfig.getInstance().getMinFileSize())
if (f.isFile()
&& f.length() > AppConfig.getInstance().getMinFileSize())
{
if (fi.getcTime().getTime() == FileTools.getFileCreateTime(new File(fi.getPath())))
if (fi.getcTime().getTime() == FileTools
.getFileCreateTime(new File(fi.getPath())))
{
if (excludeDirs != null)
{
Expand Down Expand Up @@ -116,7 +122,8 @@ private static void rotateOneFile(FileInfo fi, int angel)
{
String tmpFilePath = fi.getPath() + "_tmpfile";
new File(fi.getPath()).renameTo(new File(tmpFilePath));
ExifCreator.addExifDate(new String[] { tmpFilePath, fi.getPath(), sf.format(fi.getPhotoTime()) });
ExifCreator.addExifDate(new String[] { tmpFilePath, fi.getPath(),
sf.format(fi.getPhotoTime()) });

}
catch (Exception e)
Expand All @@ -132,8 +139,10 @@ public static long getFileCreateTime(File f) throws IOException
return -1;
}

Path path = FileSystems.getDefault().getPath(f.getParent(), f.getName());
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
Path path = FileSystems.getDefault().getPath(f.getParent(),
f.getName());
BasicFileAttributes attrs = Files.readAttributes(path,
BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
return attrs.creationTime().toMillis();
}

Expand Down Expand Up @@ -163,10 +172,12 @@ public static void rotatePhonePhoto(String fullPath, int angel)

Rectangle rect_des = new Rectangle(new Dimension(swidth, sheight));

BufferedImage res = new BufferedImage(rect_des.width, rect_des.height, BufferedImage.TYPE_INT_RGB);
BufferedImage res = new BufferedImage(rect_des.width,
rect_des.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = res.createGraphics();

g2.translate((rect_des.width - src_width) / 2, (rect_des.height - src_height) / 2);
g2.translate((rect_des.width - src_width) / 2,
(rect_des.height - src_height) / 2);
g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);

g2.drawImage(src, null, null);
Expand Down
9 changes: 6 additions & 3 deletions photoweb/src/com/backend/PerformanceStatistics.java
Expand Up @@ -7,10 +7,12 @@

public class PerformanceStatistics
{
private static final Logger logger = LoggerFactory.getLogger(PerformanceStatistics.class);
private static final Logger logger = LoggerFactory
.getLogger(PerformanceStatistics.class);
private volatile AtomicLong totalFileCount = new AtomicLong(0);
private volatile AtomicLong careFileCount = new AtomicLong(0);
private volatile AtomicLong timenow = new AtomicLong(System.currentTimeMillis());
private volatile AtomicLong timenow = new AtomicLong(
System.currentTimeMillis());

private static PerformanceStatistics instance = new PerformanceStatistics();

Expand Down Expand Up @@ -55,6 +57,7 @@ public void addOneFile(boolean isCare)
public void printPerformanceLog(long now)
{
timenow.set(now);
logger.warn("total file count: " + totalFileCount.get() + " checked file count: " + careFileCount.get());
logger.warn("total file count: " + totalFileCount.get()
+ " checked file count: " + careFileCount.get());
}
}

0 comments on commit 5a77e60

Please sign in to comment.