forked from Furtsy/node.js-image-uploader
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const express = require('express'); | ||
const multer = require('multer'); | ||
const ejs = require('ejs'); | ||
const path = require('path'); | ||
const Darkmode = require('darkmode-js'); | ||
const app = express(); | ||
const port = 3000; | ||
|
||
|
||
const storage = multer.diskStorage({ | ||
destination: './public/resim/', | ||
filename: function(req, file, cb){ | ||
cb(null,file.fieldname + '-' + Date.now() + path.extname(file.originalname)); | ||
} | ||
}); | ||
|
||
|
||
const upload = multer({ | ||
storage: storage, | ||
limits:{fileSize: 1000000}, | ||
fileFilter: function(req, file, cb){ | ||
checkFileType(file, cb); | ||
} | ||
}).single('resim'); | ||
|
||
|
||
function checkFileType(file, cb){ | ||
|
||
const filetypes = /jpeg|jpg|png|gif/; | ||
|
||
const extname = filetypes.test(path.extname(file.originalname).toLowerCase()); | ||
|
||
const mimetype = filetypes.test(file.mimetype); | ||
|
||
if(mimetype && extname){ | ||
return cb(null,true); | ||
} else { | ||
cb('Hata sadece resim!'); | ||
} | ||
} | ||
|
||
app.set('view engine', 'ejs'); | ||
|
||
app.use(express.static('./public')); | ||
|
||
app.get('/', (req, res) => res.render('index')); | ||
|
||
const requestIp = require('request-ip'); | ||
app.use(requestIp.mw()) | ||
|
||
app.post('/resim', (req, res) => { | ||
new Darkmode().showWidget(); | ||
upload(req, res, (err) => { | ||
if(err){ | ||
res.render('index', { | ||
msg: 'Dosya büyük' | ||
}); | ||
} else { | ||
if(req.file == undefined){ | ||
res.render('index', { | ||
msg: 'Dosya seçili değil' | ||
}); | ||
} else { | ||
res.render('index', { | ||
msg: 'Dosya Yüklendi!', | ||
file: `resim/${req.file.filename}`, | ||
url: ` Resimin Linki: https://your-domain/resim/${req.file.filename}` | ||
}); | ||
|
||
} | ||
} | ||
}); | ||
}); | ||
|
||
app.listen(port, () => console.log(`${port} Portundan başladı`)); |