@@ -53,7 +53,11 @@ class Schema {
53
53
private fun handleSketchUrl (uri : URI ): Editor ? {
54
54
val url = File (uri.path.replace(" /url/" , " " ))
55
55
56
- val tempSketchFolder = File (Base .untitledFolder, url.nameWithoutExtension)
56
+ val rand = (1 .. 6 )
57
+ .map { ((' a' .. ' z' ) + (' A' .. ' Z' )).random() }
58
+ .joinToString(" " )
59
+
60
+ val tempSketchFolder = File (File (Base .untitledFolder, rand), url.nameWithoutExtension)
57
61
tempSketchFolder.mkdirs()
58
62
val tempSketchFile = File (tempSketchFolder, " ${tempSketchFolder.name} .pde" )
59
63
@@ -71,7 +75,7 @@ class Schema {
71
75
?.map { it.split(" =" ) }
72
76
?.associate {
73
77
URLDecoder .decode(it[0 ], StandardCharsets .UTF_8 ) to
74
- URLDecoder .decode(it[1 ], StandardCharsets .UTF_8 )
78
+ URLDecoder .decode(it[1 ], StandardCharsets .UTF_8 )
75
79
}
76
80
? : emptyMap()
77
81
options[" data" ]?.let { data ->
@@ -81,15 +85,15 @@ class Schema {
81
85
downloadFiles(uri, code, File (sketchFolder, " code" ))
82
86
}
83
87
options[" pde" ]?.let { pde ->
84
- downloadFiles(uri, pde, sketchFolder)
88
+ downloadFiles(uri, pde, sketchFolder, " pde " )
85
89
}
86
90
options[" mode" ]?.let { mode ->
87
91
val modeFile = File (sketchFolder, " sketch.properties" )
88
92
modeFile.writeText(" mode.id=$mode " )
89
93
}
90
94
91
95
}
92
- private fun downloadFiles (uri : URI , urlList : String , targetFolder : File ){
96
+ private fun downloadFiles (uri : URI , urlList : String , targetFolder : File , extension : String = "" ){
93
97
Thread {
94
98
targetFolder.mkdirs()
95
99
@@ -101,37 +105,31 @@ class Schema {
101
105
val files = urlList.split(" ," )
102
106
103
107
files.filter { it.isNotBlank() }
104
- .map{ it.split(" :" , limit = 2 ) }
105
- .map{ segments ->
106
- if (segments.size == 2 ){
107
- if (segments[0 ].isBlank()){
108
- return @map listOf (null , segments[1 ])
109
- }
110
- return @map segments
111
- }
112
- return @map listOf (null , segments[0 ])
108
+ .map {
109
+ if (it.contains(" :" )) it
110
+ else " $it :$it "
113
111
}
112
+ .map{ it.split(" :" , limit = 2 ) }
114
113
.forEach { (name, content) ->
114
+ var target = File (targetFolder, name)
115
+ if (extension.isNotBlank() && target.extension != extension){
116
+ target = File (targetFolder, " $name .$extension " )
117
+ }
115
118
try {
116
- // Try to decode the content as base64
117
119
val file = Base64 .getDecoder().decode(content)
118
- if (name == null ){
120
+ if (name.isBlank() ){
119
121
Messages .err(" Base64 files needs to start with a file name followed by a colon" )
120
122
return @forEach
121
123
}
122
- File (targetFolder, name) .writeBytes(file)
124
+ target .writeBytes(file)
123
125
}catch (_: IllegalArgumentException ){
124
- // Assume it's a URL and download it
125
- var url = URI .create(content)
126
- if (url.host == null ){
127
- url = URI .create(" https://$base /$content " )
128
- }
129
- if (url.scheme == null ){
130
- url = URI .create(" https://$content " )
131
- }
132
-
133
- val target = File (targetFolder, name ? : url.path.split(" /" ).last())
134
- url.toURL().openStream().use { input ->
126
+ val url = URL (when {
127
+ content.startsWith(" https://" ) -> content
128
+ content.startsWith(" http://" ) -> content.replace(" http://" , " https://" )
129
+ URL (" https://$content " ).path.isNotBlank() -> " https://$content "
130
+ else -> " https://$base /$content "
131
+ })
132
+ url.openStream().use { input ->
135
133
target.outputStream().use { output ->
136
134
input.copyTo(output)
137
135
}
@@ -148,7 +146,7 @@ class Schema {
148
146
?.map { it.split(" =" ) }
149
147
?.associate {
150
148
URLDecoder .decode(it[0 ], StandardCharsets .UTF_8 ) to
151
- URLDecoder .decode(it[1 ], StandardCharsets .UTF_8 )
149
+ URLDecoder .decode(it[1 ], StandardCharsets .UTF_8 )
152
150
}
153
151
? : emptyMap()
154
152
for ((key, value) in options){
0 commit comments