Skip to content

Commit cda16f6

Browse files
authored
Merge pull request #10 from OpenRefine/issue_1_5
FilesExtensions: Support for directory selection, Removed fileContent, Project name generation
2 parents b9a86e6 + d54d6aa commit cda16f6

File tree

5 files changed

+47
-19
lines changed

5 files changed

+47
-19
lines changed

module/langs/translation-en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"files-import/no-directory-selected": "No directory selected",
77
"files-import/select-one": "Select one",
88
"files-import/scanning": "Scanning for files",
9-
"files-parsing/project-default-name": "fileList",
9+
"files-parsing/project-default-name": "folder-details",
1010
"files-import/warning-directory-selection": "You must select 1 or more directories.",
1111
"files-import/preparing": "Preparing…",
1212
"files-import/creating": "Creating project…",

module/scripts/index/files-importing-controller.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Refine.FilesImportingController.prototype.startImportingDocument = function(doc)
5858
self._doc = doc;
5959
self._jobID = data.jobID;
6060
self._options = data2.options;
61+
self._projectName = data2.projectName;
6162

6263
self._showParsingPanel();
6364
} else {
@@ -214,13 +215,14 @@ Refine.FilesImportingController.prototype._showParsingPanel = function() {
214215
delete self._doc;
215216
delete self._jobID;
216217
delete self._options;
218+
delete self._projectName;
217219

218220
self._createProjectUI.showSourceSelectionPanel();
219221
});
220222

221223
this._parsingPanelElmts.createProjectButton.click(function() { self._createProject(); });
222224

223-
this._parsingPanelElmts.projectNameInput[0].value = $.i18n('files-parsing/project-default-name');
225+
this._parsingPanelElmts.projectNameInput[0].value = self._projectName ? self._projectName : $.i18n('files-parsing/project-default-name');
224226

225227
this._createProjectUI.showCustomPanel(this._parsingPanel);
226228
this._updatePreview();

src/main/java/org/openrefine/extensions/files/importer/FilesImporter.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ public static void loadData(Project project, ProjectMetadata metadata, Importing
9191
columns.add("filePath");
9292
columns.add("filePermissions");
9393
columns.add("sha256");
94-
columns.add("fileContent");
9594
JSONUtilities.safePut(options, "columnNames", columns);
9695
JSONUtilities.safePut(options, "separator", ",");
9796

@@ -141,9 +140,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
141140
String fileExt = getFileExt(fileName);
142141
String filePermissions = getFilePermissions(file);
143142
String fileChecksum = calculateFileChecksum(file, "SHA-256");
144-
String fileContent = getFileContent(file);
145143

146-
csvPrinter.printRecord(fileName, fileSize, fileExt, dateModified, dateCreated, author, filePath, filePermissions, fileChecksum, fileContent);
144+
csvPrinter.printRecord(fileName, fileSize, fileExt, dateModified, dateCreated, author, filePath, filePermissions, fileChecksum);
147145
}
148146
} catch (Exception e) {
149147
logger.info("--- importDirectory. Error processing file: " + file + " - " + e.getMessage());
@@ -212,15 +210,28 @@ private static String bytesToHex(byte[] bytes) {
212210
}
213211

214212
private static String getFileContent(Path path) {
215-
if (Files.exists(path)) {
216-
try {
217-
String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
218-
int maxBytes = Math.min(content.length(), 1 * fileContentSizeLimit); // Max 32KB
219-
if ( canIncludeFileContent(content)) {
220-
return content.substring(0, maxBytes);
213+
if (Files.exists(path) && Files.isReadable(path)) {
214+
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
215+
StringBuilder contentBuilder = new StringBuilder();
216+
char[] buffer = new char[1024];
217+
int bytesRead;
218+
int totalBytesRead = 0;
219+
int maxBytes = fileContentSizeLimit;
220+
221+
while ((bytesRead = reader.read(buffer, 0, Math.min(buffer.length, maxBytes - totalBytesRead))) != -1) {
222+
contentBuilder.append(buffer, 0, bytesRead);
223+
totalBytesRead += bytesRead;
224+
225+
if (totalBytesRead >= maxBytes) {
226+
break;
227+
}
221228
}
222-
}
223-
catch (IOException e) {
229+
230+
String content = contentBuilder.toString();
231+
if (canIncludeFileContent(content)) {
232+
return content;
233+
}
234+
} catch (IOException e) {
224235
logger.info("--- importDirectory. Failed to read file content: " + e.getMessage());
225236
}
226237
}
@@ -323,4 +334,20 @@ private static void buildDirectoryNode(Path dir, JsonGenerator jsonGenerator) th
323334
}
324335
}
325336

337+
public static String generateProjectName(ArrayNode directoryInput) {
338+
if ( directoryInput == null || directoryInput.isEmpty() ) {
339+
return "folder-details";
340+
}
341+
String folder1 = Paths.get(directoryInput.get(0).get("directory").asText()).getFileName().toString();
342+
String folder2 = directoryInput.size() > 1 ? Paths.get(directoryInput.get(1).get("directory").asText()).getFileName().toString() : null;
343+
344+
if (folder2 == null) {
345+
return String.format("folder-details_%s", folder1);
346+
} else if (directoryInput.size() > 2) {
347+
return String.format("folder-details_%s_%s_and_more", folder1, folder2);
348+
} else {
349+
return String.format("folder-details_%s_%s", folder1, folder2);
350+
}
351+
}
352+
326353
}

src/main/java/org/openrefine/extensions/files/importer/FilesImportingController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,15 @@ private void doInitializeParserUI(HttpServletRequest request, HttpServletRespons
100100
logger.debug("::doInitializeParserUI::");
101101
}
102102

103+
ArrayNode directoryInput = ParsingUtilities.evaluateJsonStringToArrayNode(
104+
parameters.getProperty("directoryJsonValue"));
105+
String projectName = FilesImporter.generateProjectName(directoryInput);
106+
103107
ObjectNode result = ParsingUtilities.mapper.createObjectNode();
104108
ObjectNode options = ParsingUtilities.mapper.createObjectNode();
105109
JSONUtilities.safePut(result, "status", "ok");
106110
JSONUtilities.safePut(result, "options", options);
111+
JSONUtilities.safePut(result, "projectName", projectName);
107112

108113
JSONUtilities.safePut(options, "skipDataLines", 0);
109114
if(logger.isDebugEnabled()) {

src/test/java/org/google/refine/filesExtension/importer/FilesImportingControllerTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,37 +153,31 @@ private void validateLocalDirectoryTestResults(Row row) {
153153
Assert.assertEquals(row.getCellValue(2).toString(), "csv");
154154
Assert.assertNotNull(row.getCellValue(7));
155155
Assert.assertNotNull(row.getCellValue(8));
156-
Assert.assertNotNull(row.getCellValue(9));
157156
}
158157
else if ( fileCellValue.startsWith("movies")) {
159158
Assert.assertEquals(row.getCellValue(2).toString(), "tsv");
160159
Assert.assertNotNull(row.getCellValue(7));
161160
Assert.assertNotNull(row.getCellValue(8));
162-
Assert.assertNotNull(row.getCellValue(9));
163161
}
164162
else if ( fileCellValue.startsWith("dates")) {
165163
Assert.assertEquals(row.getCellValue(2).toString(), "xls");
166164
Assert.assertNotNull(row.getCellValue(7));
167165
Assert.assertNotNull(row.getCellValue(8));
168-
Assert.assertEquals(row.getCellValue(9).toString().trim().length(), 0);
169166
}
170167
else if ( fileCellValue.startsWith("euc-jp")) {
171168
Assert.assertEquals(row.getCellValue(2).toString(), "html");
172169
Assert.assertNotNull(row.getCellValue(7));
173170
Assert.assertNotNull(row.getCellValue(8));
174-
Assert.assertNotNull(row.getCellValue(9));
175171
}
176172
else if ( fileCellValue.startsWith("archive")) {
177173
Assert.assertEquals(row.getCellValue(2).toString(), "zip");
178174
Assert.assertNotNull(row.getCellValue(7));
179175
Assert.assertNotNull(row.getCellValue(8));
180-
Assert.assertEquals(row.getCellValue(9).toString().trim().length(), 0);
181176
}
182177
else if ( fileCellValue.startsWith("persons")) {
183178
Assert.assertEquals(row.getCellValue(2).toString(), "gz");
184179
Assert.assertNotNull(row.getCellValue(7));
185180
Assert.assertNotNull(row.getCellValue(8));
186-
Assert.assertEquals(row.getCellValue(9).toString().trim().length(), 0);
187181
}
188182
else {
189183
Assert.fail("Test failed : unknown record - " + row.toString());

0 commit comments

Comments
 (0)