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
@ -2382,7 +2382,7 @@ public class Base {
|
|||||||
|
|
||||||
Dimension preferredSize = fileChooser.getPreferredSize();
|
Dimension preferredSize = fileChooser.getPreferredSize();
|
||||||
fileChooser.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200));
|
fileChooser.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200));
|
||||||
|
|
||||||
int returnVal = fileChooser.showOpenDialog(editor);
|
int returnVal = fileChooser.showOpenDialog(editor);
|
||||||
|
|
||||||
if (returnVal != JFileChooser.APPROVE_OPTION) {
|
if (returnVal != JFileChooser.APPROVE_OPTION) {
|
||||||
@ -2390,8 +2390,39 @@ public class Base {
|
|||||||
}
|
}
|
||||||
|
|
||||||
File sourceFile = fileChooser.getSelectedFile();
|
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());
|
File destinationFolder = new File(getSketchbookLibrariesFolder(), sourceFile.getName());
|
||||||
if (!destinationFolder.mkdir()) {
|
if (!destinationFolder.mkdir()) {
|
||||||
editor.statusError("Can't create folder: " + sourceFile.getName() + " into libraries folder");
|
editor.statusError("Can't create folder: " + sourceFile.getName() + " into libraries folder");
|
||||||
@ -2403,15 +2434,10 @@ public class Base {
|
|||||||
editor.statusError(e);
|
editor.statusError(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu"));
|
||||||
try {
|
} finally {
|
||||||
ZipDeflater zipDeflater = new ZipDeflater(sourceFile, getSketchbookLibrariesFolder());
|
// delete zip created temp folder, if exists
|
||||||
zipDeflater.deflate();
|
FileUtils.recursiveDelete(tmpFolder);
|
||||||
} catch (IOException e) {
|
|
||||||
editor.statusError(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import java.io.File;
|
|||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class FileUtils {
|
public class FileUtils {
|
||||||
|
|
||||||
@ -66,6 +67,9 @@ public class FileUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void recursiveDelete(File file) {
|
public static void recursiveDelete(File file) {
|
||||||
|
if (file == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
for (File current : file.listFiles()) {
|
for (File current : file.listFiles()) {
|
||||||
if (current.isDirectory()) {
|
if (current.isDirectory()) {
|
||||||
@ -78,4 +82,12 @@ public class FileUtils {
|
|||||||
file.delete();
|
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