mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-17 06:52:18 +01:00
improved import library: complains if invalid zip or folders are selected
This commit is contained in:
parent
1cdf75d4df
commit
29856fff53
@ -2390,8 +2390,39 @@ public class Base {
|
||||
}
|
||||
|
||||
File sourceFile = fileChooser.getSelectedFile();
|
||||
File tmpFolder = null;
|
||||
|
||||
if (sourceFile.isDirectory()) {
|
||||
try {
|
||||
// unpack ZIP
|
||||
if (!sourceFile.isDirectory()) {
|
||||
try {
|
||||
tmpFolder = FileUtils.createTempFolder();
|
||||
ZipDeflater zipDeflater = new ZipDeflater(sourceFile, tmpFolder);
|
||||
zipDeflater.deflate();
|
||||
File[] foldersInTmpFolder = tmpFolder.listFiles(new OnlyDirs());
|
||||
if (foldersInTmpFolder.length != 1) {
|
||||
throw new IOException("Zip doesn't contain one library");
|
||||
}
|
||||
sourceFile = foldersInTmpFolder[0];
|
||||
} catch (IOException e) {
|
||||
editor.statusError(e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// is there a library?
|
||||
File libFolder = scanFatLibrary(sourceFile);
|
||||
if (libFolder == null) {
|
||||
editor.statusError("Not a valid library");
|
||||
return;
|
||||
}
|
||||
String[] headerFiles = headerListFromIncludePath(libFolder);
|
||||
if (headerFiles == null || headerFiles.length == 0) {
|
||||
editor.statusError("Not a valid library");
|
||||
return;
|
||||
}
|
||||
|
||||
// copy folder
|
||||
File destinationFolder = new File(getSketchbookLibrariesFolder(), sourceFile.getName());
|
||||
if (!destinationFolder.mkdir()) {
|
||||
editor.statusError("Can't create folder: " + sourceFile.getName() + " into libraries folder");
|
||||
@ -2403,15 +2434,10 @@ public class Base {
|
||||
editor.statusError(e);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
ZipDeflater zipDeflater = new ZipDeflater(sourceFile, getSketchbookLibrariesFolder());
|
||||
zipDeflater.deflate();
|
||||
} catch (IOException e) {
|
||||
editor.statusError(e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu"));
|
||||
} finally {
|
||||
// delete zip created temp folder, if exists
|
||||
FileUtils.recursiveDelete(tmpFolder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
@ -66,6 +67,9 @@ public class FileUtils {
|
||||
}
|
||||
|
||||
public static void recursiveDelete(File file) {
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
if (file.isDirectory()) {
|
||||
for (File current : file.listFiles()) {
|
||||
if (current.isDirectory()) {
|
||||
@ -78,4 +82,12 @@ public class FileUtils {
|
||||
file.delete();
|
||||
}
|
||||
|
||||
public static File createTempFolder() throws IOException {
|
||||
File tmpFolder = new File(System.getProperty("java.io.tmpdir"), "arduino_" + new Random().nextInt(1000000));
|
||||
if (!tmpFolder.mkdir()) {
|
||||
throw new IOException("Unable to create temp folder " + tmpFolder);
|
||||
}
|
||||
return tmpFolder;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user