This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch NLPCRAFT-483
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/NLPCRAFT-483 by this push:
     new 802722d  WIP.
802722d is described below

commit 802722d902a94fb19f75760c1fffdab8a6d7776f
Author: Sergey Kamov <[email protected]>
AuthorDate: Wed Mar 2 18:40:38 2022 +0300

    WIP.
---
 .../nlpcraft/internal/util/NCResourceReader.scala  | 41 ++++++++++++++--------
 .../apache/nlpcraft/internal/util/NCUtils.scala    |  7 ++++
 2 files changed, 34 insertions(+), 14 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCResourceReader.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCResourceReader.scala
index c5bbe25..5dca009 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCResourceReader.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCResourceReader.scala
@@ -17,8 +17,7 @@
 
 package org.apache.nlpcraft.internal.util
 
-import org.apache.nlpcraft.NCException
-
+import org.apache.nlpcraft.*
 import java.io.*
 import java.net.URL
 import scala.util.Using
@@ -26,6 +25,7 @@ import scala.io.Source
 import com.typesafe.scalalogging.LazyLogging
 import org.apache.commons.io.IOUtils
 import org.apache.commons.codec.digest.DigestUtils
+import org.apache.nlpcraft.internal.util.NCUtils.{getClass, isResource}
 
 import java.nio.file.Files
 import scala.collection.immutable.Map
@@ -48,9 +48,9 @@ object NCResourceReader extends LazyLogging:
         val f = new File(normDir)
 
         if f.exists then
-            if !f.isDirectory then throw new NCException(s"Invalid folder: 
$normDir")
+            if !f.isDirectory then E(s"Invalid folder: $normDir")
         else
-            if !f.mkdirs then throw new NCException(s"Cannot create folder: 
$normDir")
+            if !f.mkdirs then E(s"Cannot create folder: $normDir")
 
         f
 
@@ -78,19 +78,12 @@ object NCResourceReader extends LazyLogging:
       * @param f
       */
     private def delete(f: File): Unit =
-        if !f.delete() then throw new NCException(s"Couldn't delete file: 
${f.getAbsolutePath}")
+        if !f.delete() then E(s"Couldn't delete file: ${f.getAbsolutePath}")
         else logger.info(s"File deleted: ${f.getAbsolutePath}")
 
     /**
       *
       * @param f
-      * @return
-      */
-    private def isExists(f: File): Boolean = f.exists() && f.isFile
-
-    /**
-      *
-      * @param f
       * @param md5
       * @return
       */
@@ -159,11 +152,31 @@ object NCResourceReader extends LazyLogging:
                 delete(f)
                 download(path, f.getAbsolutePath, md5)
 
-        if isExists(f) then
+        // Path.
+        if NCUtils.isFile(f) then
             process(f)
         else
+            // Path in default folder.
             f = new File(DFLT_DIR, path)
-            if isExists(f) then process(f) else download(path, 
f.getAbsolutePath, md5)
+
+            if NCUtils.isFile(f) then
+                process(f)
+            else
+                // Resource.
+                if NCUtils.isResource(path) then
+                    getClass.getClassLoader.getResourceAsStream(path) match
+                        case in if in != null =>
+                            Using.resource(new FileOutputStream(f)) { out => 
IOUtils.copy(in, out) }
+
+                            process(f)
+                        case _ => E(s"Resource not found: $path")
+                // URL.
+                else if NCUtils.isUrl(path) then
+                    IOUtils.copy(new URL(path), f)
+                    process(f)
+                else
+                    // Download.
+                    download(path, f.getAbsolutePath, md5)
 
     /**
       *
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCUtils.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCUtils.scala
index b4a7f3c..b6d3dd9 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCUtils.scala
@@ -402,6 +402,13 @@ object NCUtils extends LazyLogging:
 
     /**
       *
+      * @param f
+      * @return
+      */
+    def isFile(f: File): Boolean = f.exists() && f.isFile
+
+    /**
+      *
       * @param src Local filesystem path, resources file or URL.
       */
     def getStream(src: String): InputStream =

Reply via email to