import org.apache.http.impl.bootstrap.HttpServer; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.err.println("Usage: <picturesRootFolder> [cacheFolder] [configFolder]");
System.exit(-1);
}
FileSystem fileSystem = FileSystems.getDefault();
Path rootFolder = getRootFolder(fileSystem, args[0]);
String cacheRootFolder = (args.length >= 2 ? args[1] : "cache");
String thumbnailCacheFolder = cacheRootFolder + "/thumbnails";
String fullscreenCacheFolder = cacheRootFolder + "/fullscreen";
String extractedPicturesCacheFolder = cacheRootFolder + "/extracted";
String configFile = (args.length >= 3 ? args[2] : ".") + "/photato.ini";
PhotatoConfig.init(configFile);
System.out.println("Starting photato");
System.out.println("-- Config file: " + configFile);
System.out.println("-- Cache folder: " + cacheRootFolder);
System.out.println("-- Pictures folder: " + rootFolder);
HttpServer server = getDefaultServer(fileSystem.getPath("www"));
server.start();
if (!Files.exists(fileSystem.getPath("cache"))) {
Files.createDirectory(fileSystem.getPath("cache"));
}
HttpClient httpClient = HttpClientBuilder.create().setUserAgent(serverName).build();
ExifToolDownloader.run(httpClient, fileSystem, PhotatoConfig.forceFfmpegToolsDownload);
FfmpegDownloader.run(httpClient, fileSystem, PhotatoConfig.forceExifToolsDownload);
ThumbnailGenerator thumbnailGenerator = new ThumbnailGenerator(fileSystem, rootFolder, thumbnailCacheFolder, extractedPicturesCacheFolder, PhotatoConfig.thumbnailHeight, PhotatoConfig.thumbnailQuality);
IGpsCoordinatesDescriptionGetter gpsCoordinatesDescriptionGetter = new OSMGpsCoordinatesDescriptionGetter(httpClient, PhotatoConfig.addressElementsCount);
MetadataAggregator metadataGetter = new MetadataAggregator(fileSystem, "cache/metadata.cache", gpsCoordinatesDescriptionGetter);
FullScreenImageGetter fullScreenImageGetter = new FullScreenImageGetter(fileSystem, rootFolder, fullscreenCacheFolder, extractedPicturesCacheFolder, PhotatoConfig.fullScreenPictureQuality, PhotatoConfig.maxFullScreenPictureWitdh, PhotatoConfig.maxFullScreenPictureHeight);
PhotatoFilesManager photatoFilesManager = new PhotatoFilesManager(rootFolder, fileSystem, metadataGetter, thumbnailGenerator, fullScreenImageGetter, PhotatoConfig.prefixModeOnly, PhotatoConfig.indexFolderName, PhotatoConfig.useParallelPicturesGeneration);
// Closing tmp server
server.shutdown(5, TimeUnit.SECONDS);
while (true) {
try {
server = ServerBootstrap.bootstrap()
.setListenerPort(PhotatoConfig.serverPort)
.setServerInfo(serverName)
.setSocketConfig(getSocketConfig())
.setExceptionLogger(new StdErrorExceptionLogger())
.registerHandler(Routes.rawVideosRootUrl + "/*", new VideoHandler(rootFolder, Routes.rawVideosRootUrl))
.registerHandler(Routes.rawPicturesRootUrl + "/*", new ImageHandler(rootFolder, Routes.rawPicturesRootUrl))
.registerHandler(Routes.fullScreenPicturesRootUrl + "/*", new ImageHandler(fileSystem.getPath(fullscreenCacheFolder), Routes.fullScreenPicturesRootUrl))
.registerHandler(Routes.thumbnailRootUrl + "/*", new ImageHandler(fileSystem.getPath(thumbnailCacheFolder), Routes.thumbnailRootUrl))
.registerHandler(Routes.listItemsApiUrl, new FolderListHandler(Routes.listItemsApiUrl, photatoFilesManager))
.registerHandler("/img/*", new ImageHandler(fileSystem.getPath("www/img"), "/img"))
.registerHandler("/js/*", new JsHandler(fileSystem.getPath("www/js"), "/js"))
.registerHandler("/css/*", new CssHandler(fileSystem.getPath("www/css"), "/css"))
.registerHandler("*", new DefaultHandler(fileSystem.getPath("www")))
.create();
server.start();
System.out.println("Server started on port " + server.getLocalPort() + " (http://" + getLocalIp() + ":" + server.getLocalPort() + ")");
server.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (IOException | InterruptedException ex) {
// In case of port already binded
System.err.println("Could not start the server ...");
Thread.sleep(1000);
}
}
}
请发表评论