From e686d418f86cc279475e022d5ebfa45ab010ae7c Mon Sep 17 00:00:00 2001 From: yqf3139 Date: Wed, 22 Mar 2017 20:28:48 +0800 Subject: [PATCH] Fix reading binary file error --- app/containers/FunctionCreatePage/index.js | 15 +++++++++------ app/containers/FunctionEditPage/index.js | 12 ++++++++---- app/containers/FunctionUploadPage/index.js | 4 +++- app/utils/api.js | 4 +--- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/app/containers/FunctionCreatePage/index.js b/app/containers/FunctionCreatePage/index.js index cee3d41..65d955e 100644 --- a/app/containers/FunctionCreatePage/index.js +++ b/app/containers/FunctionCreatePage/index.js @@ -17,7 +17,7 @@ import { makeSelectLoading, makeSelectError, makeSelectFunctionTest } from 'cont import { makeSelectEnvironments } from 'containers/EnvironmentsPage/selectors'; import { loadEnvironmentAction } from 'containers/EnvironmentsListPage/actions'; import { createFunctionAction, testFunctionAction, cleanTestFunctionAction } from 'containers/FunctionCreatePage/actions'; -import { slug } from 'utils/util'; +import { slug, encodeBase64 } from 'utils/util'; import commonMessages from 'messages'; export class FunctionCreatePage extends React.Component { // eslint-disable-line react/prefer-stateless-function @@ -83,16 +83,19 @@ export class FunctionCreatePage extends React.Component { // eslint-disable-line onSave() { const { item } = this.state; if (this.isFunctionRequiredInputValid(item)) { - this.props.createFunction(item); + const fn = Object.assign({}, item); + fn.code = encodeBase64(fn.code); + this.props.createFunction(fn); } } onFunctionTest(test) { - const obj = Object.assign({}, this.state.item); + const fn = Object.assign({}, this.state.item); - if (this.isFunctionRequiredInputValid(obj)) { - obj.test = test; - this.props.testFunction(obj); + if (this.isFunctionRequiredInputValid(fn)) { + fn.test = test; + fn.code = encodeBase64(fn.code); + this.props.testFunction(fn); return true; } return false; diff --git a/app/containers/FunctionEditPage/index.js b/app/containers/FunctionEditPage/index.js index 4dfd4d5..384e425 100644 --- a/app/containers/FunctionEditPage/index.js +++ b/app/containers/FunctionEditPage/index.js @@ -19,6 +19,7 @@ import { loadEnvironmentAction } from 'containers/EnvironmentsListPage/actions'; import { testFunctionAction, cleanTestFunctionAction } from 'containers/FunctionCreatePage/actions'; import { getFunctionAction, loadTriggersHttpAction, deleteTriggerHttpAction, updateFunctionAction, createTriggerHttpAction, loadKubeWatchersAction, createKubeWatcherAction, deleteKubeWatcherAction } from 'containers/FunctionEditPage/actions'; import commonMessages from 'messages'; +import { encodeBase64 } from 'utils/util'; export class FunctionEditPage extends React.Component { // eslint-disable-line react/prefer-stateless-function constructor(props) { @@ -109,9 +110,10 @@ export class FunctionEditPage extends React.Component { // eslint-disable-line r } onFunctionTest(test) { - const obj = Object.assign({}, this.state.item); - obj.test = test; - this.props.testFunction(obj); + const fn = Object.assign({}, this.state.item); + fn.code = encodeBase64(fn.code); + fn.test = test; + this.props.testFunction(fn); return true; } @@ -150,7 +152,9 @@ export class FunctionEditPage extends React.Component { // eslint-disable-line r onSave(event) { event.preventDefault(); const { item } = this.state; - this.props.updateFunction(item); + const fn = Object.assign({}, item); + fn.code = encodeBase64(fn.code); + this.props.updateFunction(fn); } render() { diff --git a/app/containers/FunctionUploadPage/index.js b/app/containers/FunctionUploadPage/index.js index 1a94093..997cb9b 100644 --- a/app/containers/FunctionUploadPage/index.js +++ b/app/containers/FunctionUploadPage/index.js @@ -124,12 +124,14 @@ export class FunctionUploadPage extends React.Component { // eslint-disable-line const reader = new FileReader(); reader.onload = (e) => { f.code = e.target.result; + // remove the header: data:application/x-go;base64, + f.code = f.code.slice(f.code.indexOf(',') + 1); counter[0] += 1; if (counter[0] === length) { that.props.setUploadFunctions(fns); } }; - reader.readAsText(f.file); + reader.readAsDataURL(f.file); delete f.file; }); } diff --git a/app/utils/api.js b/app/utils/api.js index 480062a..91cc847 100644 --- a/app/utils/api.js +++ b/app/utils/api.js @@ -3,8 +3,6 @@ */ import axios from 'axios'; -import { encodeBase64 } from './util'; - const basePath = '/proxy/controller/v1/'; const routerPath = '/proxy/router'; @@ -37,7 +35,7 @@ function checkStatus(response) { } function buildFunction(item) { - return { metadata: { name: item.name }, environment: { name: item.environment }, code: encodeBase64(item.code) }; + return { metadata: { name: item.name }, environment: { name: item.environment }, code: item.code }; } export function getEnvironments() {