@@ -53,7 +53,11 @@ class Schema {
5353 private fun handleSketchUrl (uri : URI ): Editor ? {
5454 val url = File (uri.path.replace(" /url/" , " " ))
5555
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)
5761 tempSketchFolder.mkdirs()
5862 val tempSketchFile = File (tempSketchFolder, " ${tempSketchFolder.name} .pde" )
5963
@@ -71,7 +75,7 @@ class Schema {
7175 ?.map { it.split(" =" ) }
7276 ?.associate {
7377 URLDecoder .decode(it[0 ], StandardCharsets .UTF_8 ) to
74- URLDecoder .decode(it[1 ], StandardCharsets .UTF_8 )
78+ URLDecoder .decode(it[1 ], StandardCharsets .UTF_8 )
7579 }
7680 ? : emptyMap()
7781 options[" data" ]?.let { data ->
@@ -81,15 +85,15 @@ class Schema {
8185 downloadFiles(uri, code, File (sketchFolder, " code" ))
8286 }
8387 options[" pde" ]?.let { pde ->
84- downloadFiles(uri, pde, sketchFolder)
88+ downloadFiles(uri, pde, sketchFolder, " pde " )
8589 }
8690 options[" mode" ]?.let { mode ->
8791 val modeFile = File (sketchFolder, " sketch.properties" )
8892 modeFile.writeText(" mode.id=$mode " )
8993 }
9094
9195 }
92- private fun downloadFiles (uri : URI , urlList : String , targetFolder : File ){
96+ private fun downloadFiles (uri : URI , urlList : String , targetFolder : File , extension : String = "" ){
9397 Thread {
9498 targetFolder.mkdirs()
9599
@@ -101,37 +105,31 @@ class Schema {
101105 val files = urlList.split(" ," )
102106
103107 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 "
113111 }
112+ .map{ it.split(" :" , limit = 2 ) }
114113 .forEach { (name, content) ->
114+ var target = File (targetFolder, name)
115+ if (extension.isNotBlank() && target.extension != extension){
116+ target = File (targetFolder, " $name .$extension " )
117+ }
115118 try {
116- // Try to decode the content as base64
117119 val file = Base64 .getDecoder().decode(content)
118- if (name == null ){
120+ if (name.isBlank() ){
119121 Messages .err(" Base64 files needs to start with a file name followed by a colon" )
120122 return @forEach
121123 }
122- File (targetFolder, name) .writeBytes(file)
124+ target .writeBytes(file)
123125 }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 ->
135133 target.outputStream().use { output ->
136134 input.copyTo(output)
137135 }
@@ -148,7 +146,7 @@ class Schema {
148146 ?.map { it.split(" =" ) }
149147 ?.associate {
150148 URLDecoder .decode(it[0 ], StandardCharsets .UTF_8 ) to
151- URLDecoder .decode(it[1 ], StandardCharsets .UTF_8 )
149+ URLDecoder .decode(it[1 ], StandardCharsets .UTF_8 )
152150 }
153151 ? : emptyMap()
154152 for ((key, value) in options){
0 commit comments