From 6ff352870ec1dfea573fb3e6de370f8020fe0d04 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Sun, 04 Apr 2021 08:38:03 +0000
Subject: [PATCH] Umgestellt auf nio.FileSystem

---
 src/de/uhilger/avdirektor/handler/PlayOnHandler.java |   78 ++++++++++++++++++++++++---------------
 1 files changed, 48 insertions(+), 30 deletions(-)

diff --git a/src/de/uhilger/avdirektor/handler/PlayOnHandler.java b/src/de/uhilger/avdirektor/handler/PlayOnHandler.java
index ea6dd8b..5fcf37e 100644
--- a/src/de/uhilger/avdirektor/handler/PlayOnHandler.java
+++ b/src/de/uhilger/avdirektor/handler/PlayOnHandler.java
@@ -1,69 +1,87 @@
 package de.uhilger.avdirektor.handler;
 
 import com.sun.net.httpserver.HttpExchange;
-import com.sun.net.httpserver.HttpHandler;
 import de.uhilger.avdirektor.App;
 import de.uhilger.avdirektor.OMXLogLeser;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.logging.Level;
 import java.util.logging.Logger;
-import org.apache.commons.io.FileUtils;
 
 /**
- * z.B. 
+ * z.B.
  * http://rpi4-az:9090/avd/playon?th=1&ti=240&o=local&log=true&titel=http://amd-srv:9090/srv/Filme/C/casino_royale.m4v
+ *
  * @author ulrich
  */
 public class PlayOnHandler extends PlayHandler {
+
+  private static final Logger logger = Logger.getLogger(PlayOnHandler.class.getName());
   
-  private static final Logger logger = Logger.getLogger(PlayOnHandler.class.getName());  
-  
-  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd-hh-MM-ss");
+  public static final String LOG_DIR = "omx-logs";
+  public static final String LOG_EXT = "log";
+  public static final String DASH = "-";
+  public static final String DOT = ".";
+
+  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
 
   public PlayOnHandler(String cmd) {
     super(cmd);
   }
-  
+
   /*
     1. buildParams
     2. process
-  */
-
+   */
   @Override
   protected StringBuilder buildParams(HttpExchange t) {
-    String wd = System.getProperty("omx.wd");
-    File targetDir = new File(wd, "omx-logs");
-    try {
-      File logFile = new File(wd, "omxplayer.log");
-      Date date = new Date(logFile.lastModified());
-      File srcFile = new File(wd, "omxplayer-" + sdf.format(date) + ".log");
-      logFile.renameTo(srcFile);
-      targetDir.mkdirs();
-      FileUtils.moveFileToDirectory(srcFile, targetDir, false);      
-    } catch (IOException ex) {
-      Logger.getLogger(PlayOnHandler.class.getName()).log(Level.SEVERE, null, ex);
-    }
+    File logDir = logSichern();
     StringBuilder params = super.buildParams(t);
     params.append(" --pos ");
     try {
-      // hier das Ergebnis der Log-Auswertung angeben
-      params.append(logsLesen(targetDir));
-    } catch (IOException ex) {
-      Logger.getLogger(PlayOnHandler.class.getName()).log(Level.SEVERE, null, ex);
-    } catch (ParseException ex) {
-      Logger.getLogger(PlayOnHandler.class.getName()).log(Level.SEVERE, null, ex);
+      params.append(dauerBestimmen(logDir));
+    } catch (IOException | ParseException ex) {
+      logger.log(Level.SEVERE, null, ex);
     }
     return params;
-  }  
+  }
+  
+  private File logSichern() {
+    String wd = System.getProperty(App.OMX_WD);
+    File zielOrdner = new File(wd, LOG_DIR);
+    try {
+      // omxplayer.log umbenennen
+      FileSystem fs = FileSystems.getDefault();
+      Path logDatei = fs.getPath(wd, OMXPlayer.NAME + DOT + LOG_EXT);
+      Date now = new Date();
+      String neuerName = OMXPlayer.NAME + DASH + sdf.format(now) + DOT + LOG_EXT;
+      Files.move(logDatei, logDatei.resolveSibling(neuerName));
 
-  private String logsLesen(File logDir) throws IOException, FileNotFoundException, ParseException {
+      // Unterverzeichnis ggf. erzeugen
+      zielOrdner.mkdirs();
+
+      // omxplayer-datum.log verschieben
+      Path quellDatei = fs.getPath(wd, neuerName);
+      Path zielPfad = fs.getPath(wd, LOG_DIR);
+      Files.move(quellDatei, zielPfad.resolve(quellDatei.getFileName()), REPLACE_EXISTING);
+    } catch (IOException ex) {
+      logger.log(Level.SEVERE, null, ex);
+    }
+    return zielOrdner;
+  }
+
+  private String dauerBestimmen(File logDir) throws IOException, FileNotFoundException, ParseException {
     OMXLogLeser leser = new OMXLogLeser();
     return leser.logDirLesen(logDir);
   }
-  
+
 }

--
Gitblit v1.9.3