Просмотр исходного кода

front validation, objectId import

mizozobu 6 лет назад
Родитель
Сommit
2b5bd80133

+ 5 - 1
src/client/js/components/Admin/Import/GrowiImportForm.jsx

@@ -37,7 +37,11 @@ class GrowiImportForm extends React.Component {
   }
 
   validateForm() {
-    return this.inputRef.current && this.inputRef.current.files[0] != null;
+    return (
+      this.inputRef.current // null check
+      && this.inputRef.current.files[0] // null check
+      && /\.zip$/.test(this.inputRef.current.files[0].name) // validate extension
+    );
   }
 
   render() {

+ 3 - 3
src/server/service/import.js

@@ -4,6 +4,7 @@ const path = require('path');
 const JSONStream = require('JSONStream');
 const streamToPromise = require('stream-to-promise');
 const unzip = require('unzipper');
+const { ObjectId } = require('mongoose').Types;
 
 class ImportService {
 
@@ -11,7 +12,6 @@ class ImportService {
     this.baseDir = path.join(crowi.tmpDir, 'imports');
     this.encoding = 'utf-8';
     this.per = 100;
-    this.ObjectId = require('mongoose').Types.ObjectId;
     this.keepOriginal = this.keepOriginal.bind(this);
 
     // { pages: Page, users: User, ... }
@@ -81,8 +81,8 @@ class ImportService {
    */
   keepOriginal(_value, { _document, schema, key }) {
     let value;
-    if (schema[key].instance === 'ObjectID' && this.ObjectId.isValid(_value)) {
-      value = this.ObjectId(_value);
+    if (schema[key].instance === 'ObjectID' && ObjectId.isValid(_value)) {
+      value = ObjectId(_value);
     }
     else {
       value = _value;