Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'qn' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
var qn = require('qn')
var localEnv = require('../config/local_env.json')
var path = require('path')
var client = qn.create({
accessKey: localEnv.qiniu.accessKey,
secretKey: localEnv.qiniu.secretKey,
bucket: localEnv.qiniu.bucket,
origin: localEnv.qiniu.origin
})
let getFullName = (file) => {
var approot = path.resolve(path.dirname(__dirname), 'app') + '/'
return file.split(approot)[1]
}
var qiniu = {
// 普通上传
const path = require('path');
const qn = require('qn');
const fs = require('fs');
const chalk = require('chalk');
const { qconfig } = require(path.resolve('mg.config.js')).upload;
var fileTotal = 0;
var fileUploadCount = 0;
var client = qn.create({
accessKey: qconfig.ACCESS_KEY,
secretKey: qconfig.SECRET_KEY,
bucket: qconfig.bucket,
origin: qconfig.origin,
});
function qnUpload(filePath,filename){
client.uploadFile(filePath, {key: filename}, function (err, result) {
if(err){
console.log(chalk.red(`[Error]: 上传文件失败:${err.toString()}`));
return;
}
console.log(chalk.greenBright(`[Success]: 上传文件至七牛云CDN成功!文件地址:${result.url}`));
fileUploadCount++;
// 上传完毕则退出
const qn = require('qn');
const config = require('../config');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
let storage;
if(config.qiniu.accessKey && config.qiniu.accessKey !== 'you access key') {
storage = qn.create(config.qiniu);
} else {
// 创建上传目录
mkdirp(config.upload.path, function (err) {
if(err)
throw new Error('上传文件目录创建失败!');
});
storage = {
upload (stream, options, cb) {
let filePath = path.join(config.upload.path, options.key);
stream.on('end', function () {
cb(null, {
key: options.key,
url: filePath
});
/**
* QiniuCtrl module.
* @file 七牛控制器模块
* @module controller/qiniu
* @author Surmon
*/
const qiniu = require('qn')
const CONFIG = require('app.config')
const { buildController, initController, handleSuccess } = require('np-core/np-processor')
const client = qiniu.create(CONFIG.QINIU)
const QiniuCtrl = initController()
// 获取配置列表
QiniuCtrl.GET = (req, res) => {
const result = { uptoken: client.uploadToken() }
handleSuccess({ res, result, message: 'upToken 获取成功' })
}
module.exports = buildController(QiniuCtrl)
const express = require('express')
const router = express.Router()
const fs = require('fs');
const qn = require('qn');
const User = require('../models/user');
const Good = require('../models/goods');
require('./../util/dateFormat')
// 空间名
const bucket = 'avatar-img-d';
// 七牛云
const client = qn.create({
accessKey: 'n83SaVzVtzNbZvGCz0gWsWPgpERKp0oK4BtvXS-Y',
secretKey: '1Uve9T2_gQX9pDY0BFJCa1RM_isy9rNjfC4XVliW',
bucket: bucket,
origin: 'http://ouibvkb9c.bkt.clouddn.com'
})
// 登录接口
router.post('/login', async (req, res) => {
let {userName, userPwd} = req.body;
const doc = await User.findOne({userName, userPwd});
try {
if (doc) {
const {userId, name, avatar} = doc
res.cookie("userId", userId, {
};
let validate = function(file) {
let error = '';
if (options.minFileSize && options.minFileSize > file.size) {
error = 'File is too small';
} else if (options.maxFileSize && options.maxFileSize < file.size) {
error = 'File is too big';
} else if (!options.acceptFileTypes.test(file.name)) {
error = 'Filetype not allowed';
}
return !error;
};
//七牛云存储
let client = null;
if (options.storage.type === 'qiniu') {
client = qn.create(options.storage.options);
}
let Uploader = {};
Uploader.delete = function(url, callback) {
let fileName = path.basename(decodeURIComponent(url))
console.log('删除文件', url, fileName);
if (fileName[0] !== '.') {
if(url.indexOf(options.storage.options.domain) > -1) {
try {
client.delete(fileName, function(err) {
callback && callback.call(null, err);
})
} catch(e) {
console.log('删除7牛图片失败', e);
callback && callback.call(null, '删除7牛图片失败');
}
} else {
if (bucket.isDefault) {
usingBucket = bucket.name
origin = bucket.domain
}
}
})
if (!usingBucket || !origin) {
console.log('No using bucket! Use `$ cown use ` to use a bucket.')
return
}
const accessKey = usingAccount.value().accessKey
const secretKey = usingAccount.value().secretKey
let client = qn.create({
accessKey, secretKey, origin,
bucket: usingBucket
})
let filename = source.split('/')[source.split('/').length - 1]
spin.start()
client.uploadFile(source, {key: filename }, (err, result) => {
spin.stop()
if (err) {
utils.showError(err)
} else {
console.log(colors.green('Upload success!'))
console.log(result.url)
if (options.copy) {
ncp.copy(result.url)
router.get('/', function (req, res, next) {
var qn = require('qn');
var client = qn.create(simditor_qn_config.qn);
var token = client.uploadToken();
console.log(token)
res.render('demo/bootstrap_table', {
title: 'index'
});
});
/*
* @Dsc:上传打包后静态文件到七牛(后续扩展成通用的webpack插件)
* @Author: hubin
* @Date: 2017-11-25 00:46:16
*/
const qn = require('qn');
const path = require('path');
const fs = require('fs');
const config_qn = require('../config/config').qiniu;
const root = path.resolve(__dirname, '../public/static/');
const uploadArr = [];
let client = qn.create(config_qn);
function explorer(path){
return new Promise((res, rej) => {
fs.readdir(path, function(err, files){
//err错误 , files 文件名列表包含文件夹与文件
if(err){
console.log('error:\n' + err);
rej(-1)
return;
}
files.forEach(function(file){
fs.stat(path + '/' + file, function(err, stat){
if(err){console.log(err); return;}
if(stat.isDirectory()){
constructor(opts) {
this.options = Object.assign(config.upload, opts);
this.checkExists(this.options.tmpDir);
this.checkExists(this.options.uploadDir);
this.client = qn.create(this.options.storage.options);
}