Display latest image

The latest image gets displayed for 60 seconds, after that all images
get displayed randomly.
This commit is contained in:
mandlm 2015-12-28 13:24:32 +01:00
parent f2c9265d5f
commit 3ef1435e12
2 changed files with 48 additions and 3 deletions

View File

@ -1,6 +1,10 @@
package net.molez.mandlm.fotostream.server; package net.molez.mandlm.fotostream.server;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Random; import java.util.Random;
import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.google.gwt.user.server.rpc.RemoteServiceServlet;
@ -10,12 +14,52 @@ import net.molez.mandlm.fotostream.client.CurrentImageURLService;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class CurrentImageURLServiceImpl extends RemoteServiceServlet implements CurrentImageURLService public class CurrentImageURLServiceImpl extends RemoteServiceServlet implements CurrentImageURLService
{ {
private List<File> imageFiles = new ArrayList<File>();
private void readImageFiles()
{
imageFiles.clear();
File imageFolder = new File("img/");
File[] files = imageFolder.listFiles();
for (File imageFile : files)
{
if (imageFile.isFile() && imageFile.getName().endsWith(".jpg"))
{
imageFiles.add(imageFile);
}
}
imageFiles.sort(new Comparator<File>()
{
@Override
public int compare(File file0, File file1)
{
Long file0Date = file0.lastModified();
Long file1Date = file1.lastModified();
return file0Date.compareTo(file1Date);
}
});
}
public String getCurrentImageURL() public String getCurrentImageURL()
{ {
File imageFolder = new File("img/"); int newImageSeconds = 60;
File[] fileList = imageFolder.listFiles(); readImageFiles();
return fileList[new Random().nextInt(fileList.length)].getPath(); File latestFile = imageFiles.get(imageFiles.size() - 1);
Date latestFileDate = new Date(latestFile.lastModified());
if (latestFileDate.before(new Date(new Date().getTime() - newImageSeconds * 1000)))
{
return imageFiles.get(new Random().nextInt(imageFiles.size())).getPath();
}
else
{
return latestFile.getPath();
}
} }
} }

View File

@ -3,4 +3,5 @@ div
{ {
padding: 20px; padding: 20px;
background-color: black; background-color: black;
color: green;
} }