Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / FileExtensionFunctions.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.core
18
19 import kotlinx.coroutines.Dispatchers
20 import kotlinx.coroutines.withContext
21 import org.apache.commons.io.FileUtils
22 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintArchiveUtils
23 import java.io.File
24 import java.io.InputStream
25 import java.nio.charset.Charset
26 import java.nio.file.Path
27 import java.nio.file.Paths
28
29 fun InputStream.toFile(path: String): File {
30     val file = File(path)
31     file.outputStream().use { this.copyTo(it) }
32     return file
33 }
34
35 fun File.reCreateDirs(): File {
36     if (this.exists()) {
37         this.deleteRecursively()
38     }
39     // this.mkdirs()
40     FileUtils.forceMkdir(this)
41     check(this.exists()) {
42         throw BluePrintException("failed to re create dir(${this.absolutePath})")
43     }
44     return this
45 }
46
47 fun File.compress(targetZipFileName: String): File {
48     return this.compress(Paths.get(targetZipFileName).toFile())
49 }
50
51 /**
52  * Compress the current Dir to the target zip file and return the target zip file
53  */
54 fun File.compress(targetZipFile: File): File {
55     BluePrintArchiveUtils.compress(this, targetZipFile)
56     return targetZipFile
57 }
58
59 fun File.deCompress(targetFileName: String): File {
60     return this.deCompress(Paths.get(targetFileName).toFile())
61 }
62
63 /**
64  * De-Compress the current zip file to the target file and return the target file
65  */
66 fun File.deCompress(targetFile: File): File {
67     BluePrintArchiveUtils.deCompress(this, targetFile.path)
68     return targetFile
69 }
70
71 fun deleteDir(path: String, vararg more: String?) {
72     normalizedFile(path, *more).deleteRecursively()
73 }
74
75 fun checkFileExists(file: File, lazyMessage: () -> Any) {
76     if (!file.exists()) {
77         val message = lazyMessage()
78         throw IllegalStateException(message.toString())
79     }
80 }
81
82 fun normalizedFile(path: String, vararg more: String?): File {
83     return Paths.get(path, *more).toFile().normalize()
84 }
85
86 fun normalizedPath(path: String, vararg more: String?): Path {
87     return Paths.get(path, *more).toAbsolutePath().normalize()
88 }
89
90 fun normalizedPathName(path: String, vararg more: String?): String {
91     return normalizedPath(path, *more).toString()
92 }
93
94 suspend fun File.reCreateNBDirs(): File = withContext(Dispatchers.IO) {
95     reCreateDirs()
96 }
97
98 suspend fun deleteNBDir(path: String, vararg more: String?): Boolean = withContext(Dispatchers.IO) {
99     normalizedFile(path, *more).deleteRecursively()
100 }
101
102 suspend fun File.readNBText(): String = withContext(Dispatchers.IO) {
103     readText(Charset.defaultCharset())
104 }
105
106 suspend fun File.readNBLines(): List<String> = withContext(Dispatchers.IO) {
107     readLines(Charset.defaultCharset())
108 }