Skip to content

Commit 3087931

Browse files
committed
improved import library: complains if invalid zip or folders are selected
1 parent 2a401f2 commit 3087931

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

app/src/processing/app/Base.java

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,16 +2613,47 @@ public void handleAddLibrary(Editor editor) {
26132613

26142614
Dimension preferredSize = fileChooser.getPreferredSize();
26152615
fileChooser.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200));
2616-
2616+
26172617
int returnVal = fileChooser.showOpenDialog(editor);
26182618

26192619
if (returnVal != JFileChooser.APPROVE_OPTION) {
26202620
return;
26212621
}
26222622

26232623
File sourceFile = fileChooser.getSelectedFile();
2624+
File tmpFolder = null;
2625+
2626+
try {
2627+
// unpack ZIP
2628+
if (!sourceFile.isDirectory()) {
2629+
try {
2630+
tmpFolder = FileUtils.createTempFolder();
2631+
ZipDeflater zipDeflater = new ZipDeflater(sourceFile, tmpFolder);
2632+
zipDeflater.deflate();
2633+
File[] foldersInTmpFolder = tmpFolder.listFiles(new OnlyDirs());
2634+
if (foldersInTmpFolder.length != 1) {
2635+
throw new IOException("Zip doesn't contain one library");
2636+
}
2637+
sourceFile = foldersInTmpFolder[0];
2638+
} catch (IOException e) {
2639+
editor.statusError(e);
2640+
return;
2641+
}
2642+
}
2643+
2644+
// is there a library?
2645+
File libFolder = scanFatLibrary(sourceFile);
2646+
if (libFolder == null) {
2647+
editor.statusError("Not a valid library");
2648+
return;
2649+
}
2650+
String[] headerFiles = headerListFromIncludePath(libFolder);
2651+
if (headerFiles == null || headerFiles.length == 0) {
2652+
editor.statusError("Not a valid library");
2653+
return;
2654+
}
26242655

2625-
if (sourceFile.isDirectory()) {
2656+
// copy folder
26262657
File destinationFolder = new File(getSketchbookLibrariesFolder(), sourceFile.getName());
26272658
if (!destinationFolder.mkdir()) {
26282659
editor.statusError("Can't create folder: " + sourceFile.getName() + " into libraries folder");
@@ -2634,15 +2665,10 @@ public void handleAddLibrary(Editor editor) {
26342665
editor.statusError(e);
26352666
return;
26362667
}
2637-
} else {
2638-
try {
2639-
ZipDeflater zipDeflater = new ZipDeflater(sourceFile, getSketchbookLibrariesFolder());
2640-
zipDeflater.deflate();
2641-
} catch (IOException e) {
2642-
editor.statusError(e);
2643-
return;
2644-
}
2668+
editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu"));
2669+
} finally {
2670+
// delete zip created temp folder, if exists
2671+
FileUtils.recursiveDelete(tmpFolder);
26452672
}
2646-
editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu"));
26472673
}
26482674
}

app/src/processing/app/helpers/FileUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.FileInputStream;
55
import java.io.FileOutputStream;
66
import java.io.IOException;
7+
import java.util.Random;
78

89
public class FileUtils {
910

@@ -66,6 +67,9 @@ public static void copy(File sourceFolder, File destFolder) throws IOException {
6667
}
6768

6869
public static void recursiveDelete(File file) {
70+
if (file == null) {
71+
return;
72+
}
6973
if (file.isDirectory()) {
7074
for (File current : file.listFiles()) {
7175
if (current.isDirectory()) {
@@ -78,4 +82,12 @@ public static void recursiveDelete(File file) {
7882
file.delete();
7983
}
8084

85+
public static File createTempFolder() throws IOException {
86+
File tmpFolder = new File(System.getProperty("java.io.tmpdir"), "arduino_" + new Random().nextInt(1000000));
87+
if (!tmpFolder.mkdir()) {
88+
throw new IOException("Unable to create temp folder " + tmpFolder);
89+
}
90+
return tmpFolder;
91+
}
92+
8193
}

0 commit comments

Comments
 (0)