Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions .angular-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"name": "wstie"
},
"apps": [{
"root": "side_client",
"outDir": "dist_client",
"root": "app/public",
"outDir": "dist/browser",
"assets": [
"assets"
],
Expand All @@ -16,19 +16,47 @@
"prefix": "app",
"styles": [
"styles.less",
"../node_modules/froala-editor/css/froala_editor.pkgd.min.css",
"../node_modules/froala-editor/css/froala_style.min.css"
"../../node_modules/froala-editor/css/froala_editor.pkgd.min.css",
"../../node_modules/froala-editor/css/froala_style.min.css"
],
"scripts": [
"../node_modules/froala-editor/js/froala_editor.pkgd.min.js",
"../node_modules/froala-editor/js/languages/zh_cn.js"
"../../node_modules/froala-editor/js/froala_editor.pkgd.min.js",
"../../node_modules/froala-editor/js/languages/zh_cn.js"
],
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}],
},
{
"platform": "server",
"root": "app/public",
"outDir": "dist/server",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.server.ts",
"polyfills": "polyfills.ts",
"tsconfig": "tsconfig.server.json",
"prefix": "app",
"styles": [
"styles.less",
"../../node_modules/froala-editor/css/froala_editor.pkgd.min.css",
"../../node_modules/froala-editor/css/froala_style.min.css"
],
"scripts": [
"../../node_modules/froala-editor/js/froala_editor.pkgd.min.js",
"../../node_modules/froala-editor/js/languages/zh_cn.js"
],
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}],
"defaults": {
"styleExt": "less"
}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ Thumbs.db

# Ignore built ts files
dist_*/**/*
dist/**/*
devServer/**/*
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 全栈项目
前端angular 4.3.4
node 8.0.0
此项目前后独立,未有同构,ts开发,去webpack化
前端angular 5
node 9.6
同构优化中....
216 changes: 108 additions & 108 deletions side_server/config/imageUpload.ts → app/config/imageUpload.ts
Original file line number Diff line number Diff line change
@@ -1,108 +1,108 @@
import * as Busboy from 'busboy';
import * as path from "path";
import * as fs from "fs";
import * as sha1 from "sha1";
// Gets a filename extension.
let getExtension =(filename)=> {
return filename.split(".").pop();
}
// Test if a image is valid based on its extension and mime type.
let isImageValid =(filename, mimetype)=> {
let allowedExts = ["gif", "jpeg", "jpg", "png", "svg", "blob"];
let allowedMimeTypes = ["image/gif", "image/jpeg", "image/pjpeg", "image/x-png", "image/png", "image/svg+xml"];
// Get image extension.
let extension = getExtension(filename);
return allowedExts.indexOf(extension.toLowerCase()) != -1 &&
allowedMimeTypes.indexOf(mimetype) != -1;
}
export let upload = (req, callback)=>{
let busboy:any;
// The route on which the image is saved.
var fileRoute = "/uploads/";
// Server side file path on which the image is saved.
var saveToPath = null;
// Flag to tell if a stream had an error.
var hadStreamError = null;
// Used for sending response.
var link = null;
// Stream error handler.
let handleStreamError =(error)=>{
// Do not enter twice in here.
if (hadStreamError) {
return;
}
hadStreamError = error;
// Cleanup: delete the saved path.
if (saveToPath) {
return fs.unlink(saveToPath, function(err) {
return callback(error);
});
}
return callback(error);
}
// Instantiate Busboy.
try {
busboy = new Busboy({ headers: req.headers });
} catch (e) {
return callback(e);
}
// Handle file arrival.
busboy.on("file", (fieldname, file, filename, encoding, mimetype)=>{
// Check fieldname:
if ("file" != fieldname) {
// Stop receiving from this stream.
file.resume();
return callback("Fieldname is not correct. It must be file .");
}
// Generate link.
var randomName = sha1(new Date().getTime()) + "." + getExtension(filename);
link = fileRoute + randomName;
// Generate path where the file will be saved.
var appDir = path.dirname(require.main.filename);
saveToPath = path.join(appDir, link);
// Pipe reader stream (file from client) into writer stream (file from disk).
file.on("error", handleStreamError);
// Create stream writer to save to file to disk.
var diskWriterStream = fs.createWriteStream(saveToPath);
diskWriterStream.on("error", handleStreamError);
// Validate image after it is successfully saved to disk.
diskWriterStream.on("finish", ()=>{
// Check if image is valid
var status = isImageValid(saveToPath, mimetype);
if (!status) {
return handleStreamError("File does not meet the validation.");
}
return callback(null, { link: link });
});
// Save image to disk.
file.pipe(diskWriterStream);
});
// Handle file upload termination.
busboy.on("error", handleStreamError);
req.on("error", handleStreamError);
// Pipe reader stream into writer stream.
return req.pipe(busboy);
}
import * as Busboy from 'busboy';
import * as path from "path";
import * as fs from "fs";
import sha1 from "sha1";

// Gets a filename extension.
let getExtension =(filename)=> {
return filename.split(".").pop();
}

// Test if a image is valid based on its extension and mime type.
let isImageValid =(filename, mimetype)=> {
let allowedExts = ["gif", "jpeg", "jpg", "png", "svg", "blob"];
let allowedMimeTypes = ["image/gif", "image/jpeg", "image/pjpeg", "image/x-png", "image/png", "image/svg+xml"];

// Get image extension.
let extension = getExtension(filename);

return allowedExts.indexOf(extension.toLowerCase()) != -1 &&
allowedMimeTypes.indexOf(mimetype) != -1;
}

export let upload = (req, callback)=>{
let busboy:any;
// The route on which the image is saved.
var fileRoute = "/uploads/";

// Server side file path on which the image is saved.
var saveToPath = null;

// Flag to tell if a stream had an error.
var hadStreamError = null;

// Used for sending response.
var link = null;

// Stream error handler.
let handleStreamError =(error)=>{
// Do not enter twice in here.
if (hadStreamError) {
return;
}

hadStreamError = error;

// Cleanup: delete the saved path.
if (saveToPath) {
return fs.unlink(saveToPath, function(err) {
return callback(error);
});
}

return callback(error);
}

// Instantiate Busboy.
try {
busboy = new Busboy({ headers: req.headers });
} catch (e) {
return callback(e);
}

// Handle file arrival.
busboy.on("file", (fieldname, file, filename, encoding, mimetype)=>{
// Check fieldname:
if ("file" != fieldname) {
// Stop receiving from this stream.
file.resume();
return callback("Fieldname is not correct. It must be file .");
}

// Generate link.
var randomName = sha1(String(new Date().getTime())) + "." + getExtension(filename);
link = fileRoute + randomName;

// Generate path where the file will be saved.
var appDir = path.dirname(require.main.filename);
saveToPath = path.join(appDir, link);

// Pipe reader stream (file from client) into writer stream (file from disk).
file.on("error", handleStreamError);

// Create stream writer to save to file to disk.
var diskWriterStream = fs.createWriteStream(saveToPath);
diskWriterStream.on("error", handleStreamError);

// Validate image after it is successfully saved to disk.
diskWriterStream.on("finish", ()=>{
// Check if image is valid
var status = isImageValid(saveToPath, mimetype);
if (!status) {
return handleStreamError("File does not meet the validation.");
}

return callback(null, { link: link });
});

// Save image to disk.
file.pipe(diskWriterStream);
});

// Handle file upload termination.
busboy.on("error", handleStreamError);
req.on("error", handleStreamError);

// Pipe reader stream into writer stream.
return req.pipe(busboy);
}
Loading