Browse Source

Merge pull request #211 from crowi/upload-anyfiletype

Enable anytype of file upload
Sotaro KARASAWA 9 years ago
parent
commit
bf4888f62c

+ 1 - 0
lib/crowi/express-init.js

@@ -71,6 +71,7 @@ module.exports = function(crowi, app) {
         language:   User.getLanguageLabels(),
         registrationMode: Config.getRegistrationModeLabels(),
     };
+    res.locals.local_config = Config.getLocalconfig(config); // config for browser context
 
     next();
   });

+ 2 - 1
lib/form/admin/app.js

@@ -5,6 +5,7 @@ var form = require('express-form')
 
 module.exports = form(
   field('settingForm[app:title]').required(),
-  field('settingForm[app:confidential]')
+  field('settingForm[app:confidential]'),
+  field('settingForm[app:fileUpload]').isInt()
 );
 

+ 31 - 2
lib/models/config.js

@@ -23,6 +23,8 @@ module.exports = function(crowi) {
       'app:title'         : 'Crowi',
       'app:confidential'  : '',
 
+      'app:fileUpload'    : false,
+
       'security:registrationMode'      : 'Open',
       'security:registrationWhiteList' : [],
 
@@ -37,8 +39,6 @@ module.exports = function(crowi) {
       'mail:smtpUser'     : '',
       'mail:smtpPassword' : '',
 
-      'searcher:url': '',
-
       'google:clientId'     : '',
       'google:clientSecret' : '',
 
@@ -180,6 +180,17 @@ module.exports = function(crowi) {
     return method != 'none';
   };
 
+  configSchema.statics.fileUploadEnabled = function(config)
+  {
+    const Config = this;
+
+    if (!Config.isUploadable(config)) {
+      return false;
+    }
+
+    return config.crowi['app:fileUpload'] || false;
+  };
+
   configSchema.statics.hasSlackConfig = function(config)
   {
     if (!config.notification) {
@@ -206,6 +217,24 @@ module.exports = function(crowi) {
     return true;
   };
 
+  configSchema.statics.getLocalconfig = function(config)
+  {
+    const Config = this;
+
+    const local_config = {
+      crowi: {
+        title: config.crowi['app:title'],
+        url: config.crowi['app:url'] || '',
+      },
+      upload: {
+        image: Config.isUploadable(config),
+        file: Config.fileUploadEnabled(config),
+      },
+    };
+
+    return local_config;
+  }
+
   /*
   configSchema.statics.isInstalled = function(config)
   {

+ 0 - 1
lib/routes/attachment.js

@@ -145,7 +145,6 @@ module.exports = function(crowi, app) {
             page: page.toObject(),
             attachment: data.toObject(),
             url: fileUrl,
-            filename: fileUrl, // this is for inline-attachemnets plugin http://inlineattachment.readthedocs.io/en/latest/pages/configuration.html
             pageCreated: pageCreated,
           };
 

+ 20 - 0
lib/views/admin/app.html

@@ -52,6 +52,26 @@
           </div>
         </div>
 
+        <div class="form-group">
+          <div class="col-xs-offset-3 col-xs-6">
+            <input type="checkbox" name="settingForm[app:fileUpload]" value="1"
+              {% if settingForm['app:fileUpload'] %}
+              checked
+              {% endif %}
+              {% if not isUploadable() %}
+              disabled="disabled"
+              {% else %}
+              {% endif %}
+              >
+              <label for="settingForm[app:fileUpload]" class="">画像以外のファイルアップロードを許可</label>
+
+              <p class="help-block">
+                ファイルアップロードの設定を有効にしている場合にのみ、選択可能です。<br>
+                許可をしている場合、画像以外のファイルをページに添付可能になります。
+              </p>
+          </div>
+        </div>
+
         <div class="form-group">
           <div class="col-xs-offset-3 col-xs-6">
             <input type="hidden" name="_csrf" value="{{ csrf() }}">

+ 4 - 0
lib/views/layout/layout.html

@@ -142,6 +142,10 @@
 </body>
 {% endblock %}
 
+<script type="application/json" id="crowi-context-hydrate">
+{{ local_config|json|safe }}
+</script>
+
 <script src="{{ assets('/js/crowi.js') }}"></script>
 <script src="{{ assets('/js/app.js') }}"></script>
 </html>

+ 7 - 0
resource/css/_attachments.scss

@@ -2,6 +2,13 @@
 .page-attachments {
   .attachment-in-use {
     margin: 0 0 0 4px;
+    padding: 1px 5px;
+  }
+
+  .attachment-filetype {
+    margin: 0 0 0 4px;
+    padding: 1px 5px;
+    font-weight: normal;
   }
 
   .attachment-delete {

+ 1 - 0
resource/js/app.js

@@ -33,6 +33,7 @@ const crowi = new Crowi({
   csrfToken: $('#content-main').data('csrftoken'),
 }, window);
 window.crowi = crowi;
+crowi.setConfig(JSON.parse(document.getElementById('crowi-context-hydrate').textContent || '{}'));
 crowi.fetchUsers();
 
 const crowiRenderer = new CrowiRenderer();

+ 4 - 0
resource/js/components/PageAttachment/Attachment.js

@@ -33,6 +33,8 @@ export default class Attachment extends React.Component {
       fileInUse = <span className="attachment-in-use label label-info">In Use</span>;
     }
 
+    const fileType = <span className="attachment-filetype label label-default">{attachment.fileFormat}</span>;
+
     return (
       <li>
           <User user={attachment.creator} />
@@ -40,6 +42,8 @@ export default class Attachment extends React.Component {
 
           <a href={attachment.url}> {attachment.originalName}</a>
 
+          {fileType}
+
           {fileInUse}
 
           <a className="text-danger attachment-delete" onClick={this._onAttachmentDeleteClicked}><Icon name="trash-o" /></a>

+ 21 - 2
resource/js/crowi-form.js

@@ -425,12 +425,31 @@ $(function() {
         _csrf: csrfToken
       },
       progressText: '(Uploading file...)',
-      urlText: "\n![file]({filename})\n"
+      jsonFieldName: 'url',
     };
 
+    // if files upload is set
+    var config = crowi.getConfig();
+    if (config.upload.file) {
+      attachmentOption.allowedTypes = '*';
+    }
+
+    attachmentOption.remoteFilename = function(file) {
+      return file.name;
+    };
+
+    attachmentOption.onFileReceived = function(file) {
+      // if not image
+      if (!file.type.match(/^image\/.+$/)) {
+        // modify urlText with 'a' tag
+        this.settings.urlText = `[${file.name}]({filename})\n`;
+      } else {
+        this.settings.urlText = `![${file.name}]({filename})\n`;
+      }
+    }
+
     attachmentOption.onFileUploadResponse = function(res) {
       var result = JSON.parse(res.response);
-      console.log(result);
 
       if (result.ok && result.pageCreated) {
         var page = result.page,

+ 9 - 0
resource/js/util/Crowi.js

@@ -7,6 +7,7 @@ import axios from 'axios'
 export default class Crowi {
   constructor(context, window) {
     this.context = context;
+    this.config = {};
     this.csrfToken = context.csrfToken;
 
     this.location = window.location || {};
@@ -33,6 +34,14 @@ export default class Crowi {
     return context;
   }
 
+  setConfig(config) {
+    this.config = config;
+  }
+
+  getConfig() {
+    return this.config;
+  }
+
   recoverData() {
     const keys = [
       'userByName',