alaska-field-image
Advanced tools
Comparing version 0.11.21 to 0.12.0
106
index.js
@@ -1,30 +0,39 @@ | ||
// @flow | ||
'use strict'; | ||
import { Field } from 'alaska'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import moment from 'moment'; | ||
import mime from 'mime'; | ||
import mkdirp from 'mkdirp'; | ||
import mongoose from 'mongoose'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
const ObjectId = mongoose.Types.ObjectId; | ||
var _alaska = require('alaska'); | ||
export default class ImageField extends Field { | ||
static plain = mongoose.Schema.Types.Mixed; | ||
static viewOptions = ['multi', 'allowed']; | ||
static defaultOptions = { | ||
cell: 'ImageFieldCell', | ||
view: 'ImageFieldView', | ||
dir: 'public/uploads/', | ||
pathFormat: 'YYYY/MM/DD/', | ||
prefix: '/uploads/', | ||
allowed: ['jpg', 'png', 'gif'] | ||
}; | ||
var _fs = require('fs'); | ||
dir: string; | ||
pathFormat: string; | ||
prefix: string; | ||
allowed: string[]; | ||
var _fs2 = _interopRequireDefault(_fs); | ||
var _path = require('path'); | ||
var _path2 = _interopRequireDefault(_path); | ||
var _moment = require('moment'); | ||
var _moment2 = _interopRequireDefault(_moment); | ||
var _mimeTypes = require('mime-types'); | ||
var _mimeTypes2 = _interopRequireDefault(_mimeTypes); | ||
var _mkdirp = require('mkdirp'); | ||
var _mkdirp2 = _interopRequireDefault(_mkdirp); | ||
var _mongoose = require('mongoose'); | ||
var _mongoose2 = _interopRequireDefault(_mongoose); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const ObjectId = _mongoose2.default.Types.ObjectId; | ||
class ImageField extends _alaska.Field { | ||
/** | ||
@@ -42,9 +51,12 @@ * 上传 | ||
} | ||
// $Flow | ||
let name = file.filename || ''; | ||
// $Flow | ||
let ext = (file.ext || '').toLowerCase(); | ||
let mimeType = file.mime || file.mimeType; | ||
// $Flow | ||
let mimeType = file.mime || file.mimeType || ''; | ||
let filePath; | ||
let buffer; | ||
let { dir } = field; | ||
let local = field.dir; | ||
let dir = field.dir; | ||
let url = field.prefix; | ||
@@ -69,4 +81,4 @@ let id = new ObjectId(); | ||
//文件路径 | ||
mimeType = mime.lookup(file); | ||
name = path.basename(file); | ||
mimeType = _mimeTypes2.default.lookup(file); | ||
name = _path2.default.basename(file); | ||
filePath = file; | ||
@@ -82,3 +94,3 @@ } else if (file.path) { | ||
if (!ext && name) { | ||
ext = path.extname(name); | ||
ext = _path2.default.extname(name); | ||
if (ext) { | ||
@@ -89,5 +101,5 @@ ext = ext.substr(1).toLowerCase(); | ||
if (!ext) { | ||
ext = mime.extension(mimeType).replace('jpeg', 'jpg'); | ||
ext = _mimeTypes2.default.extension(mimeType).replace('jpeg', 'jpg'); | ||
} | ||
if (field.allowed.indexOf(ext) < 0) { | ||
if (field.allowed && field.allowed.indexOf(ext) < 0) { | ||
reject(new Error('Image format error')); | ||
@@ -98,3 +110,4 @@ return; | ||
function writeFile() { | ||
fs.writeFile(local, buffer, (error) => { | ||
// $Flow | ||
_fs2.default.writeFile(local, buffer, error => { | ||
if (error) { | ||
@@ -119,3 +132,3 @@ reject(error); | ||
if (field.pathFormat) { | ||
img.path += moment().format(field.pathFormat); | ||
img.path += (0, _moment2.default)().format(field.pathFormat); | ||
} | ||
@@ -129,6 +142,6 @@ dir += img.path; | ||
fs.stat(dir, (e) => { | ||
_fs2.default.stat(dir, e => { | ||
if (e) { | ||
//文件夹不存在 | ||
mkdirp(dir, (err) => { | ||
(0, _mkdirp2.default)(dir, err => { | ||
if (err) { | ||
@@ -147,4 +160,6 @@ reject(err); | ||
if (filePath) { | ||
fs.readFile(file.path, onReadFile); | ||
// $Flow | ||
_fs2.default.readFile(file.path, onReadFile); | ||
} else { | ||
// $Flow | ||
onReadFile(null, file); | ||
@@ -157,3 +172,3 @@ } | ||
let field = this; | ||
let schema: Mongoose$Schema = this._schema; | ||
let schema = this._schema; | ||
let defaultValue = field.default || {}; | ||
@@ -179,3 +194,3 @@ | ||
let imageSchema = new mongoose.Schema(paths); | ||
let imageSchema = new _mongoose2.default.Schema(paths); | ||
@@ -192,3 +207,3 @@ if (field.multi) { | ||
let record = this; | ||
return ImageField.upload(file, field).then((img) => { | ||
return ImageField.upload(file, field).then(img => { | ||
record.set(field.path, img); | ||
@@ -204,5 +219,16 @@ return Promise.resolve(); | ||
} | ||
return (value || []).map((v) => (v && v.url ? v.url : '')).filter((v) => v); | ||
return (value || []).map(v => v && v.url ? v.url : '').filter(v => v); | ||
}); | ||
} | ||
} | ||
exports.default = ImageField; | ||
ImageField.plain = _mongoose2.default.Schema.Types.Mixed; | ||
ImageField.viewOptions = ['multi', 'allowed']; | ||
ImageField.defaultOptions = { | ||
cell: 'ImageFieldCell', | ||
view: 'ImageFieldView', | ||
dir: 'public/uploads/', | ||
pathFormat: 'YYYY/MM/DD/', | ||
prefix: '/uploads/', | ||
allowed: ['jpg', 'png', 'gif'] | ||
}; |
{ | ||
"name": "alaska-field-image", | ||
"version": "0.11.21", | ||
"version": "0.12.0", | ||
"description": "Alaska image field", | ||
@@ -9,12 +9,13 @@ "keywords": [ | ||
], | ||
"alaska": "field", | ||
"main": "index.js", | ||
"repository": "https://github.com/maichong/alaska/tree/master/packages/alaska-field-image", | ||
"repository": "https://github.com/maichong/alaska/tree/master/src/alaska-field-image", | ||
"author": "Liang <liang@maichong.it> (https://github.com/liangxingchen)", | ||
"license": "MIT", | ||
"dependencies": { | ||
"mime": "^1.3.6", | ||
"mime-types": "^2.1.17", | ||
"mkdirp": "^0.5.1", | ||
"moment": "^2.18.1", | ||
"moment": "^2.19.2", | ||
"qs": "*" | ||
} | ||
} |
@@ -5,9 +5,4 @@ // @flow | ||
export default class ImageFieldCell extends React.Component { | ||
props: { | ||
value: Object | ||
}; | ||
shouldComponentUpdate(props: Object) { | ||
export default class ImageFieldCell extends React.Component<Alaska$view$Field$Cell$Props> { | ||
shouldComponentUpdate(props: Alaska$view$Field$Cell$Props) { | ||
return props.value !== this.props.value; | ||
@@ -14,0 +9,0 @@ } |
@@ -9,4 +9,9 @@ // @flow | ||
export default class ImageFieldView extends React.Component { | ||
type State = { | ||
max: number, | ||
errorText: string, | ||
multi: any | ||
}; | ||
export default class ImageFieldView extends React.Component<Alaska$view$Field$View$Props, State> { | ||
static contextTypes = { | ||
@@ -17,22 +22,5 @@ settings: PropTypes.object, | ||
props: { | ||
className: string, | ||
model: Object, | ||
field: Object, | ||
data: Object, | ||
errorText: string, | ||
disabled: boolean, | ||
value: any, | ||
onChange: Function, | ||
}; | ||
state: { | ||
max: number, | ||
errorText: string, | ||
multi: any | ||
}; | ||
imageInput: any; | ||
constructor(props: Object) { | ||
constructor(props: Alaska$view$Field$View$Props) { | ||
super(props); | ||
@@ -49,3 +37,3 @@ this.state = { | ||
componentWillReceiveProps(nextProps: Object) { | ||
componentWillReceiveProps(nextProps: Alaska$view$Field$View$Props) { | ||
let newState = {}; | ||
@@ -58,4 +46,4 @@ if (nextProps.errorText !== undefined) { | ||
shouldComponentUpdate(props: Object, state: Object) { | ||
return !shallowEqualWithout(props, this.props, 'data', 'onChange', 'model') | ||
shouldComponentUpdate(props: Alaska$view$Field$View$Props, state: State) { | ||
return !shallowEqualWithout(props, this.props, 'record', 'onChange', 'model') | ||
|| !shallowEqualWithout(state, this.state); | ||
@@ -65,5 +53,7 @@ } | ||
handleAddImage = () => { | ||
const t = this.context.t; | ||
let { model, field, data, value } = this.props; | ||
let multi = field.multi; | ||
const { t } = this.context; | ||
let { | ||
model, field, record, value | ||
} = this.props; | ||
let { multi } = field; | ||
if (value) { | ||
@@ -78,12 +68,8 @@ if (!multi) { | ||
} | ||
let serviceId = 'alaska-user'; | ||
let modelName = 'User'; | ||
let id = '_new'; | ||
if (model) { | ||
serviceId = model.serviceId; | ||
modelName = model.name; | ||
let { serviceId, modelName } = model || { serviceId: 'alaska-user', name: 'User' }; | ||
if (record && record._id) { | ||
id = record._id; | ||
} | ||
if (data && data._id) { | ||
id = data._id; | ||
} | ||
let nextState = { | ||
@@ -121,5 +107,4 @@ errorText: '' | ||
handleRemoveItem(item: any) { | ||
let multi = this.props.field.multi; | ||
let value = null; | ||
if (multi) { | ||
if (this.props.field.multi) { | ||
value = []; | ||
@@ -139,3 +124,5 @@ _.forEach(this.props.value, (i) => { | ||
render() { | ||
let { className, field, value, disabled } = this.props; | ||
let { | ||
className, field, value, disabled | ||
} = this.props; | ||
let { errorText, max } = this.state; | ||
@@ -149,12 +136,16 @@ if (!field.multi) { | ||
_.forEach(value, (item, index) => { | ||
items.push(<div key={index} className="image-field-item"> | ||
<img alt="" src={item.thumbUrl} /> | ||
{ | ||
readonly ? null : <button | ||
className="btn btn-link btn-block" | ||
disabled={disabled} | ||
onClick={() => this.handleRemoveItem(item)} | ||
>删除</button> | ||
} | ||
</div>); | ||
items.push(( | ||
<div key={index} className="image-field-item"> | ||
<img alt="" src={item.thumbUrl} /> | ||
{ | ||
readonly ? null : ( | ||
<button | ||
className="btn btn-link btn-block" | ||
disabled={disabled} | ||
onClick={() => this.handleRemoveItem(item)} | ||
>删除</button> | ||
) | ||
} | ||
</div> | ||
)); | ||
}); | ||
@@ -164,12 +155,16 @@ if (items.length < max) { | ||
if (!readonly) { | ||
items.push(<div className="image-field-item image-field-add" key="add"> | ||
<i className="fa fa-plus-square-o" /> | ||
<input | ||
ref={(r) => { this.imageInput = r; }} | ||
multiple={this.state.multi} | ||
accept="image/png;image/jpg;" | ||
type="file" | ||
onChange={this.handleAddImage} | ||
/> | ||
</div>); | ||
items.push(( | ||
<div className="image-field-item image-field-add" key="add"> | ||
<i className="fa fa-plus-square-o" /> | ||
<input | ||
ref={(r) => { | ||
this.imageInput = r; | ||
}} | ||
multiple={this.state.multi} | ||
accept="image/png;image/jpg;" | ||
type="file" | ||
onChange={this.handleAddImage} | ||
/> | ||
</div> | ||
)); | ||
} | ||
@@ -179,8 +174,10 @@ } | ||
if (!items.length && readonly) { | ||
items.push(<div className="image-field-item image-field-add" key="add"> | ||
<i className="fa fa-picture-o" /> | ||
</div>); | ||
items.push(( | ||
<div className="image-field-item image-field-add" key="add"> | ||
<i className="fa fa-picture-o" /> | ||
</div> | ||
)); | ||
} | ||
let help = field.help; | ||
let { help } = field; | ||
className += ' image-field'; | ||
@@ -187,0 +184,0 @@ if (errorText) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
12630
396
+ Addedmime-types@^2.1.17
+ Addedhas-symbols@1.0.3(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
- Removedmime@^1.3.6
- Removedhas-symbols@1.1.0(transitive)
- Removedmime@1.6.0(transitive)
Updatedmoment@^2.19.2