Skip to content

Commit

Permalink
feat: v1.0.7 fix some bugs and upload await.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangerzi committed Feb 28, 2021
1 parent 94254d1 commit 8428dd3
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 71 deletions.
2 changes: 1 addition & 1 deletion dist/modelbox-sdk.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ <h1>modelbox-sdk</h1>
}

// 【UI】做个 loading
var layerIndex = layui.layer.load(1);
var layerIndex = layui.layer.msg("文件上传中...", {time: 0, shade: 0.3});
// 2. 拿到回调之后提交数据库保存
MODELBOX_SDK.updateToken('5c06b6b8a548d8911e3ccdbdc518fac6');
MODELBOX_SDK.uploadModel({
Expand Down
106 changes: 49 additions & 57 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import api from './src/api/modelApi'

let MODELBOX_SDK = {}
let VERSION = '0.0.1'
let VERSION = '1.0.7'
const TYPE_FILE_MAP = {
'stp': ['.stp', '.step', '.zip'],
'iges': ['.igs', '.iges', '.zip'],
Expand All @@ -23,97 +23,89 @@ function makeObject(object) {
* error
* }
*/
function uploadModel(opt = {}) {
let file = opt.file;
let type = opt.type;
let config = opt.config;
let modelConfig = opt.modelConfig;
let success = opt.success;
let error = opt.error;
async function uploadModel(opt = {}) {
let file = opt.file
let type = opt.type
let config = opt.config
let modelConfig = opt.modelConfig
let success = opt.success
let error = opt.error
// 1. check file
if (!file || !file instanceof File) {
if (error && error.apply) {
error.apply(this, ['file params need File'])
}
return false;
return false
}
// 1.1 limit 100Mb
if (file.size / 1024 / 1024 > 100) {
if (error && error.apply) {
error.apply(this, ['File size limit at 100Mb'])
}
return false;
return false
}

// 2. check file ext
const fileName = file.name;
const splitName = fileName.split('.');
const fileName = file.name
const splitName = fileName.split('.')
if (!splitName || !splitName.length) {
if (error && error.apply) {
error.apply(this, ['File ext not found'])
}
return false;
return false
}
const ext = '.' + splitName[splitName.length - 1];
const ext = '.' + splitName[splitName.length - 1]
if (!TYPE_FILE_MAP[type] || !TYPE_FILE_MAP[type].includes(ext.toLowerCase())) {
if (error && error.apply) {
error.apply(this, ['Model ' + type + ' only support ' + TYPE_FILE_MAP[type].join(',')])
}
return false;
return false
}

config = Object.assign({
'name': "Default Model Name",
'comment': "No Comment",
}, config);
config['type'] = type;
config['config'] = modelConfig;
api.createFile({
is_public: 1,
ext: ext,
}).then(data => {
}, config)
config['type'] = type
config['config'] = modelConfig

try {
let data = await api.createFile({
is_public: 1,
ext: ext,
})
if (!data || !data.upload_url) {
if (error && error.apply) {
error.apply(this, ['Generate upload url failed:' + res])
}
throw new Error("Generate upload url failed")
}
const url = data.upload_url;
const fileId = data.id;
const url = data.upload_url
const fileId = data.id
// 1. upload file
api.putFile(url, file).then(() => {
// 2. handle upload file callback
// 2. save model config
config['source_file_id'] = fileId;
api.createModel(config).then(d => {
if (success && success.apply) {
success.apply(this, [d])
}
}).catch(res => {
if (error && error.apply) {
error.apply(this, ['create model failed:' + res])
}
return false;
});

// em... oss can't notify automatic now
// setTimeout(() => {
// api.uploadComplete(fileId).catch((res) => {
// if (error && error.apply) {
// error.apply(this, ['Complete upload url failed:' + res])
// }
// });
// }, 3000);
});
}).catch(res => {
try {
await api.putFile(url, file);
} catch (res) {
throw new Error("Put File " + res)
}
// 2. handle upload file callback
// 2. save model config
config['source_file_id'] = fileId;
try {
let d = await api.createModel(config)
if (success && success.apply) {
success.apply(this, [d])
}
} catch (res) {
throw new Error("Create Model info " + res)
}
return true
} catch (res) {
if (error && error.apply) {
error.apply(this, ['Generate upload url failed:' + res])
error.apply(this, [res.toString()])
}
return false;
});
}
}

object.remainAccessTime = api.remainAccessTime;
object.updateToken = api.updateToken;
object.remainAccessTime = api.remainAccessTime
object.updateToken = api.updateToken
object.uploadModel = uploadModel
object.version = VERSION

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "./node_modules/.bin/webpack --config webpack.prod.js",
"dev": "./node_modules/.bin/webpack-dev-server --hot --config webpack.dev.js"
},
"author": "",
"author": "github.com/wangerzi",
"license": "MIT",
"dependencies": {
"axios": "^0.21.0",
Expand Down
14 changes: 9 additions & 5 deletions src/api/modelApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ async function updateToken(token) {
return setToken(token)
}

async function createFile(config = {}) {
function createFile(config = {}) {
config = Object.assign({
is_public: 1,
ext: '.zip'
Expand Down Expand Up @@ -51,13 +51,17 @@ async function createModel(config) {
}))
}

async function putFile(url, file) {
function putFile(url, file) {
return new Promise(((resolve, reject) => {
const reader = new FileReader()
reader.readAsArrayBuffer(file)
reader.onload = (e) => {
putObject(url, e.target.result)
resolve(true)
reader.onload = async (e) => {
try {
await putObject(url, e.target.result)
resolve(true)
} catch (res) {
reject(res)
}
}
reader.onerror = () => {
reject('Load file Error')
Expand Down
12 changes: 6 additions & 6 deletions src/api/requests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as axios from 'axios';
import {get as axiosGet, post as axiosPost, put as axiosPut} from 'axios';

export const API_URL = 'https://api.mobox3d.com';
// export const API_URL = 'http://localhost:8088';
Expand All @@ -7,14 +7,14 @@ let API_TOKEN = ''
export function setToken(token) {
API_TOKEN = token
}
export async function get(url, option = {}) {
export function get(url, option = {}) {
return new Promise(((resolve, reject) => {
option = Object.assign(option, {
headers: {
"X-api-key": API_TOKEN
}
})
axios.get(url, option).then((res) => {
axiosGet(url, option).then((res) => {
const data = res.data;
if (data.code !== 200) {
reject(data.message);
Expand All @@ -27,15 +27,15 @@ export async function get(url, option = {}) {
}));
}

export async function post(url, postData, option = {}) {
export function post(url, postData, option = {}) {
return new Promise(((resolve, reject) => {
option = Object.assign(option, {
headers: {
"X-api-key": API_TOKEN
}
})
// automatic json post
axios.post(url, postData, option).then((res) => {
axiosPost(url, postData, option).then((res) => {
const data = res.data;
if (data.code !== 200) {
reject(data.message, data);
Expand All @@ -49,7 +49,7 @@ export async function post(url, postData, option = {}) {
}

export async function putObject(url, body) {
axios.put(url, body, {
return axiosPut(url, body, {
headers: {
'Content-Type': '',
}
Expand Down

0 comments on commit 8428dd3

Please sign in to comment.