SlideShare a Scribd company logo
Appendix-I (Program Code)
File.js // Collecting data from the users
(sender)
import _ from 'lodash'
class File {
constructor(app) {
this.app = app;
this.model = {
name: null, originalName: null, mimeType: null, size: null,
etag: null,
created: new Date(),
} }
initWithObject(object){
this.model.name = _.get(object, 'key');
this.model.originalName = _.get(object, 'originalname');
this.model.mimeType = _.get(object, 'mimetype');
this.model.size = _.get(object, 'size');
this.model.etag= _.get(object, 'etag');
this.model.created = new Date();
return this;
} toJSON(){
return this.model;
} save(callback) {
const db = this.app.get('db');
db.collection('files').insertOne(this.model, (err, result) => {
return callback(err, result);
});}
}
export default File;
--------------------------------------------------------------------------------------------------------------------
Archiver.js // Download all the files as a zip file
import archiver from 'archiver'
import _ from 'lodash'
import path from 'path'
import S3 from './s3'
export default class FileArchiver{
constructor(app, files = [], response){
this.app = app; this.files = files; this.response = response; }
download(){
const app = this.app;
const files = this.files;
//const uploadDir = app.get('storageDir');
const zip = archiver('zip'); const response = this.response;
response.attachment('download.zip');
zip.pipe(response); const s3Downloader = new S3(app, response);
_.each(files, (file) => {
const fileObject = s3Downloader.getObject(file);
zip.append(fileObject, {name: _.get(file, 'originalName')});
});
zip.finalize();
return this;
}}
--------------------------------------------------------------------------------------------------------------------
Config.js // Sending Notification Alert through Email
import {accessKeyId,secretAccessKey} from './s3-config.json'
export const smtp = {
host: 'smtp.sendgrid.net',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: 'apikey', // generated ethereal user
pass: 'SG.HXUZZppORTSHzQuQwC-5EA.OgpSmEJnvXwo2qBGX-
ujkk7OqB1hSw2Kwlxal9abLUI' // generated ethereal password
}}
export const url = 'http://localhost:3001';
export const s3Config = {
accessKeyId: 'AKIAI7PBFWK7ZNHYIGQA',
secretAccessKey: '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'}
export const S3Region = 'us-east-1' export const s3Bucket = 'fileapp0'
--------------------------------------------------------------------------------------------------------------------
Database.js // Connecting the database through Mongodb
import {MongoClient} from 'mongodb';
const url ='mongodb://localhost:27017/fileapp';
export const connect=(callback)=>{
MongoClient.connect(url, (err, db) => {
return callback(err, db);
}); };
--------------------------------------------------------------------------------------------------------------------
Index.js // Monitor for the file present in package.JSON
import http from 'http';
import express from 'express';
import {connect} from "./database";
import AppRouter from './router'
import multer from 'multer'
import path from 'path';
import nodemailer from 'nodemailer';
import {smtp, s3Config, S3Region, s3Bucket} from './config'
// Amazon S3 Setup
import AWS from 'aws-sdk'
import multerS3 from 'multer-s3'
AWS.config.update(s3Config);
AWS.config.region = S3Region ;
const s3 = new AWS.S3();
// setup Email
let email = nodemailer.createTransport(smtp);
//File Storage Config
const storageDir = path.join(__dirname,'..', 'storage');
// const upload = multer({ storage: storageConfig })
const upload = multer({
storage: multerS3({
s3: s3,
bucket: s3Bucket,
metadata: function (req, file, cb) {
cb(null, {fieldName: file.fieldname}); },
key: function (req, file, cb) {
const filename = `${Date.now().toString()}-${file.originalname}`;
cb(null, filename)
} }) })
//End File Storage Config
const PORT = 3000;
const app = express();
app.server = http.createServer(app);
app.use(morgan('dev'));
app.use(cors({
exposedHeaders: "*"}));
app.set('root', __dirname);
app.set('storageDir',storageDir);
app.upload = upload;
app.email = email;
app.s3 = s3;
//Connect to database
connect((err,db)=> {
if (err) {
console.log("An error connecting to the database");
throw(err); }
app.set('db',db);
//init router
new AppRouter(app);
app.server.listen(process.env.PORT || PORT, () => {
console.log(`App is running on port ${app.server.address().port}`); });
});
export default app;
--------------------------------------------------------------------------------------------------------------------
Router.js // JS library that provide an API for handling routes
import {version} from '../package.json'
import path from 'path'
import _ from 'lodash'
import {ObjectID} from 'mongodb'
import File from './models/file'
import Post from './models/post'
import FileArchiver from './archiver'
import Email from './email'
import S3 from './s3'
class AppRouter {
constructor(app) {
this.app = app;
this.setupRouters();
}
setupRouters() {
const app = this.app;
const uploadDir = app.get('storageDir');
const upload = app.upload;
const db = app.get('db');
//root routing
app.get('/', (req, res, next) => {
return res.status(200).json({
version: version
}); });
//Upload Routing
app.post('/api/upload', upload.array('files'), (req, res, next) => {
const files = _.get(req, 'files', []);
console.log("files objects from s3 multer", files);
let fileModels = [];
_.each(files, (fileObject) => {
const newFile = new File(app).initWithObject(fileObject).toJSON();
fileModels.push(newFile);
});
if (fileModels.length) { //check for empty file
db.collection('files').insertMany(fileModels, (err, result) => {
if (err) {
return res.status(503).json({
error: {
message: "Unable to save your file",
} }); }
let post = new Post(app).initWithObject({
from: _.get(req,'body.from'),
to: _.get(req,'body.to'),
message: _.get(req,'body.message'),
phone: _.get(req,'body.phone'),
files: result.insertedIds,
}).toJSON();
//let save post objects to posts collection.
db.collection('posts').insertOne(post,(err,result)=>{
if(err){
return res.status(503).json({error:{message:"Your upload could not be saved"}});
}
//implement email sending to user with download link.
//send email
const sendEmail = new Email(app).sendDownloadLink(post, (err, info) => {
});
// callback to react app with post detail.
return res.json(post);
}); });
} else {
return res.status(503).json({
error: {message: "files upload is required"}
}); } });
// Download file from S3 service
const file = _.get(result, '[0]');
const downloader = new S3(app, res);
// return downloader.download(file); Proxy download from s3 service
// Download Directly from S3
const downloadUrl = downloader.getDownloadUrl(file);
return res.redirect(downloadUrl); }); });
//routing for post detail/api/posts/:id
app.get('/api/posts/:id',(req,res,next)=>{
const postId=_.get(req,'params.id');
this.getPostById(postId, (err, result) => {
if(err){
return res.status(404).json({error:{message: 'File not found.'}});
}
return res.json(result); }) });
// Routing download zip files.
app.get('/api/posts/:id/download', (req, res, next) => {
const id = _.get(req, 'params.id', null);
this.getPostById(id, (err, result) => {
if (err) {
return res.status(404).json({error: {message: 'File not found.'}});
}
const files = _.get(result, 'files', []);
const archiver = new FileArchiver(app, files, res).download();
return archiver; }) }); }
getPostById(id, callback = () => {}){
const app= this.app;
const db = app.get('db');
let postObjectId=null;
try{
postObjectId=new ObjectID(id);
}
catch (err){
return callback(err, null);
}
db.collection('posts').find({_id:postObjectId}).limit(1).toArray((err,results)=>{
let result =_.get(results,'[0]');
if(err || !result){
return callback(err ? err: new Error("File not found"));
}
const fileIds=_.get(result,'files',[]);
db.collection('files').find({_id:{$in: fileIds}}).toArray((err,files)=>{
if(err || !files || !files.length){
return callback(err ? err: new Error("File not found"));
}
result.files=files;
return callback(null, result);
}); }) }} export default AppRouter;
----------------------------------------------------------------------------------------------------------------------
S3.js // Connecting the S3 with Dpliot for Cloud Application
import _ from 'lodash'
import {s3Bucket} from './config'
export default class S3{
constructor(app, response){
this.app = app;
this.response = response;
}
getObject(file){
const s3 = this.app.s3; const options ={
Bucket: s3Bucket,
Key: _.get(file, 'name')
};
return s3.getObject(options).createReadStream(); }
download(file){
const s3 = this.app.s3;
const response = this.response;
// get Object from S3 service
const filename = _.get(file, 'originalName');
response.attachment(filename);
const options ={
Bucket: s3Bucket,
Key: _.get(file, 'name')
};
const fileObject = s3.getObject(options).createReadStream();
fileObject.pipe(response); }
getDownloadUrl(file){
const s3 = this.app.s3;
const options ={
Bucket: s3Bucket,
Key: _.get(file, 'name'),
Expires: 3600, // one hour expires. };
const url = s3.getSignedUrl('getObject', options);
return url; }}
--------------------------------------------------------------------------------------------------------------------
Header.js // Designing Dashboard through React
import React,{Component} from 'react'
class Header extends Component {
render() {
return (
<div className={'app-header'}>
<div className={'app-site-info'}>
<h1><i className={'icon-paper-plane'} /> Dpilot</h1>
<div className={'site-title'}>SHARE YOUR FILE.</div>
<div className={'site-slogan'}>SECURE ! SAFE ! FREE !</div>
</div>
</div>) }}export default Header;
----------------------------------------------------------------------------------------------------------------------
Home-form.js // Container collecting the credentials of the user as a form
import React, {Component} from 'react'
import _ from 'lodash'
import classNames from 'classnames'
import {upload} from '../helpers/upload'
import PropTypes from 'prop-types'
class HomeForm extends Component{
constructor(props){ super(props); this.state = { form: {
files: [],
to: '', from: '', phone: '', message: ''
}, errors:{
to: null, from: null, phone:null, message: null, files:
null,
} };
this._onTextChange = this._onTextChange.bind(this);
this._onSubmit = this._onSubmit.bind(this);
this._formValidation = this._formValidation.bind(this);
this._onFileAdded = this._onFileAdded.bind(this);
this._onFileRemove = this._onFileRemove.bind(this)
}
_isEmail(emailAddress) {
const emailRegex =
/^(([^<>()[].,;:s@"]+(.[^<>()[].,;:s@"]+)*)|(".+"))@(([^<>()[].,;:s@"]+.)+[^<>()[].,;:s@"]{2,
})$/i;
return emailRegex.test(emailAddress);
}
_isPhone(phoneNumber){
const phoneRegex = /^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
return phoneRegex.test(phoneNumber);
}
_formValidation(fields = [], callback = () => {}){
let {form, errors} = this.state;
_onSubmit(event){
event.preventDefault();
this._formValidation(['from', 'to','phone','files'], (isValid) => {
if(isValid){
//the form is valid and ready to submit.
const data = this.state.form;
if(this.props.onUploadBegin){
this.props.onUploadBegin(data);
}
upload(data, (event) => {
if(this.props.onUploadEvent){
this.props.onUploadEvent(event);
} }) } }); }
_onTextChange(event){
let{form} = this.state;
const fieldName = event.target.name;
const fieldValue = event.target.value;
form[fieldName] = fieldValue;
this.setState({form: form});
}
render(){
const {form,errors}= this.state;
const {files} = form;
return(
<div className={'app-card'}> <form onSubmit={this._onSubmit}>
<div className={'app-card-header'}> <div className={'app-card-header-inner'}>
{
files.length ? <div className={'app-files-selected'}>
{ files.map((file, index) => {
return (
<div key= {index} className={'app-files-selected-item'}>
<div className={'filename'}>{file.name}</div>
<div className={'file-action'}>
<button onClick={() => this._onFileRemove(index)}
type={'button'} className={'app-file-remove'}>X
</button> </div> </div> ) }) } </div> : null }
<div className={classNames('app-file-select-zone',{'error':_.get(errors,'files')})}>
<label htmlFor={'input-file'}>
<input onChange={this._onFileAdded} id={'input-file'} type="file"
multiple={true} />
{ files.length ? <span className={'app-upload-description text-
uppercase'}>Add more files</span> : <span>
<span className={'app-upload-icon'}><i className={'icon-picture-
o'} /> </span>
<span className={'app-upload-description'}>Drag and drop your files
here.</span>
</span> } </label>
</div> </div> </div>
<div className={classNames('app-form-item',{'error':_.get(errors,'phone')})}>
<label htmlFor={'phone'}>Receiver's Contact Number</label>
<input value={_.get(form, 'phone' )} onChange={this._onTextChange}
name={'phone'} placeholder={_.get(errors,'phone') ? _.get(errors,'phone'):'Your Contact Number'}
type={'text'} id={'phone'} maxLength={'10'}/>
</div>
<div className={'app-form-item'}>
<label htmlFor={'message'}>Message</label>
<textarea value= {_.get(form, 'message', '')} onChange={this._onTextChange}
name={'message'} placeholder={'Add a note (optional)'} id={'message'}/>
</div>
<div className={'app-form-actions'}>
<button type={'submit'} className={'app-button primary'}>Send</button>
</div> </div> </div> </form> </div>
) }}
HomeForm.propTypes = {
onUploadBegin: PropTypes.func,
onUploadEvent: PropTypes.func
};
export default HomeForm;
--------------------------------------------------------------------------------------------------------------------
Home-upload-sent.js // Container confirming the files sent to the receiver
import React,{Component} from 'react'
import PropTypes from 'prop-types'
import _ from 'lodash'
import {history} from "../history";
class HomeUploadSent extends Component{
constructor(props){
super(props);
}
render(){
const {data}=this.props;
console.log( "Data",data);
const to=_.get(data,'to');
const postId=_.get(data,'_id');
return(
<div className={'app-card app-card-upload-sent'}>
<div className={'app-card-content'}>
<div className={'app-card-content-inner'}>
<div className={'app-home-uploading'}>
<div className={'app-home-upload-sent-icon'}>
<i className={'icon-paper-plane'} />
</div>
<div className={'app-upload-sent-message app-text-center'}>
<h2>Files Sent! </h2>
<p>We have sent an email to {to} with a download link.The link will expire in 30
days</p> </div>
<div className={'app-upload-sent-actions app-form-actions'}>
<button onClick={()=>{
history.push( `/share/${postId}`)
}} className={'app-button primary'} type={'button '}>View File </button>
<button onClick={()=>{
if(this.props.onSendAnotherFile){
this.props.onSendAnotherFile(true); }
}} className={'app-button'} type={'button'}> Send Another File </button>
</div> </div> </div> </div> </div> ) }}
HomeUploadSent.propTypes={
data: PropTypes.object,
onSendAnotherFile: PropTypes.func }
export default HomeUploadSent;
----------------------------------------------------------------------------------------------------------------------
Upload.js //Container confirming the status of the uploaded files
import axios from 'axios'
import {apiUrl} from "../config";
import _ from 'lodash'
export const upload = (form, callback = () => {}) => {
const url = `${apiUrl}/upload`;
let files = _.get(form, 'files', []);
let data = new FormData();
_.each(files, (file) => {
data.append('files', file);
}); // upload successful.
axios.post(url, data, config).then((response) => {
return callback({
type: 'success',
payload: response.data
}) }).catch((err) => {
return callback({
type: 'error',
payload: err
}) }); };
----------------------------------------------------------------------------------------------------------------------
GoogleConfig.php // Checking the Google Configuration of the user.
<?php
session_start();
require_once "GoogleAPI/vendor/autoload.php";
$gClient = new Google_Client();
$gClient->setClientId("643208222747-
snjv039ocno2kjcfvpfebvorn6ojovnk.apps.googleusercontent.com");
$gClient->setClientSecret("SHtRqIGKQFhQLY1A78g2hzdu");
$gClient->setApplicationName("CPI Login Tutorial");
$gClient->setRedirectUri("http://localhost/test/g-callback.php");
$gClient->addScope("https://www.googleapis.com/auth/plus.login
https://www.googleapis.com/auth/userinfo.email");?>
----------------------------------------------------------------------------------------------------------------------
Connect.php // Connect to DB
<?php
$connection=mysqli_connect('localhost','root','');
if(!$connection){
echo"failed";}
$dbSelect=mysqli_select_db($connection,'demo');
if(!$dbSelect){
echo"failed selection";
die(mysqli_error($connection)); }?>
----------------------------------------------------------------------------------------------------------------------
gCallback.php //Google Callback
<?php
require_once "config.php";
if (isset($_SESSION['access_token']))
$gClient->setAccessToken($_SESSION['access_token']);
else if (isset($_GET['code'])) {
$token = $gClient->fetchAccessTokenWithAuthCode($_GET['code']);
$_SESSION['access_token'] = $token;
} else {
header('Location: index11.php');
exit(); }
$oAuth = new Google_Service_Oauth2($gClient);
$userData = $oAuth->userinfo_v2_me->get();
$_SESSION['id'] = $userData['id'];
$_SESSION['email'] = $userData['email'];
$_SESSION['gender'] = $userData['gender'];
$_SESSION['picture'] = $userData['picture'];
$_SESSION['familyName'] = $userData['familyName'];
$_SESSION['givenName'] = $userData['givenName'];
header('Location: http://www.facebook.com');
exit();
?>
----------------------------------------------------------------------------------------------------------------------
Login.php // Accepting the credentials for logging in of the user.
<?php
if(isset($_POST["submit"])){
require_once('connect.php');
if(isset($_POST) & !empty($_POST)){
$uname=$_POST['username'];
$password=$_POST['password'];
$sql="SELECT * FROM usermanagement WHERE username='".$uname."' AND
password='".$password."'";
$result=mysqli_query($connection,$sql);
if(mysqli_num_rows($result)==1){
header("Location: http://www.google.com");
exit();
}else
{ $message="we r done "; } } }
?>
<?php
if(isset($_POST["recover"])){
require_once('connect.php');
if(isset($_POST) & !empty($_POST)){
$email=$_POST["eemail"];
$data="SELECT * FROM usermanagement WHERE email='".$email."'";
$rresult=mysqli_query($connection,$data);
if(mysqli_num_rows($rresult)==1){
$row= mysqli_fetch_assoc($rresult);
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'sharma.deepanshu97@gmail.com';
$mail->Password = 'Deep@21081998';
$mail->setFrom('sharma.deepanshu97@gmail.com', 'Deepanshu Shamra');
$mail->addAddress( $row["email"]);
$mail->Subject = 'Password:Dpilot';
$mail->Body = "Your Password to access Dpilot is:" .$row["password"];
if ($mail->send())
$mess= "A mail containting password of your account is sent to you.";
else
$mess="something went wrong";
}else{
$mess="Please enter the registered email";
}}} ?>
<html>
<head>
<title>Dpilot Login/Signup </title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" type="text/css" href="fp.css">
<link rel="stylesheet" type="text/css" href="recover_style.css">
<body> <div class="loginbox"> <img src="sampleD.png" class="avatar">
<h1 > Login Here</h1> <form method="post" action="#"> <p>Username</p>
<input type="text" name="username" placeholder ="Enter Username" value="<?php
if(isset($uname) and!empty($uname)) {echo $uname;}?>" required>
<p>Password</p>
<input type="password" name="password" placeholder="Enter Password"value="" required >
<div> <?php if(isset($message) and!empty($message)){echo $message;} ?> </div>
<input type="submit" name="submit" value="Login">
<a href="dex.php" class="link">Dont Have an Account?</a><br>
<a class="link1" href="#modal" class="modal-open" id="modal-open">Lost Your
Password?</a></form>
<div class="modal" id="modal"> <div class="modal-content">
<a href="" class="modal-close">&times;</a> <div class="lloginbox">
<img src="sampleD.png" class="aavatar"> <h1 > Password Recovery</h1>
<form action="index11.php#modal" method="post">
<p id="ww">Registered Email</p>
<input type="text" name="eemail" placeholder="Enter Email">
<input type="submit" name="recover" value="Recover Password">
<div> <?php if(isset($mess) and!empty($mess)){echo $mess;} ?> </div>
</form> </div> </div> </div> </div>
</body> </head> </html>
----------------------------------------------------------------------------------------------------------------------
Signup.php // Accepting the credentials for signing up of the new user.
<?php
require_once('connect.php');
if(isset($_POST) & !empty($_POST)){
$username= $_POST['username'];
$email= $_POST['email'];
$password= $_POST['password'];
$repassword= $_POST['password2'];
if(!$username OR !$email OR !$password)
{ $message="Please Fill All the details";}
else{
if($password==$repassword){
{$sql="INSERT INTO usermanagement (username,email,password) VALUES
('$username','$email','$password')";
$result=mysqli_query($connection,$sql);
header("Location: http://www.google.com");
}
if($result){
$message="success";
}else
$message="Username already exist";}
else
$message="Password doesnot match";
}}?>
<?php
require_once "config.php";
if (isset($_SESSION['access_token'])) {
header('Location: https://056e1c1a.ngrok.io/test/dex.php');
exit(); }
$loginURL = $gClient->createAuthUrl();?>
<?php
require_once "Fconfig.php";
if (isset($_SESSION['access_token'])) {
header(' Location: https://056e1c1a.ngrok.io/test/logout.php');
exit(); }
$redirectURL = " https://056e1c1a.ngrok.io/test/f-callback.php";
$permissions = ['email'];
$FloginURL = $helper->getLoginUrl($redirectURL, $permissions);
?> <html>
<head> <title> Dpilot Signup </title>
<link rel="stylesheet" type="text/css" href="styl.css">
</head>
<body>
<div id="login-box">
<div id="left-box">
<form class="form-signin" method="post" >
<h1>Sign Up</h1>
<div id="al"> <?php if(isset($message) and!empty($message)){echo $message;} ?>
</div>
<input type="text" name="username" placeholder="Username" value="<?php if(isset($result)
and!empty($result)){if($result) echo "";}else if(isset($username) and!empty($username)) {echo
$username;} ?>" required/>
<input type="text" name="email" placeholder="Email" value="<?php if(isset($result)
and!empty($result)){if($result) echo "";}else if(isset($email) and!empty($email)) {echo $email;}
?>" required/>
<input type="password" name="password" placeholder="Password" />
<input type="password" name="password2" placeholder="Retype Password"/>
<input type="submit" name="signup-button" value="Sign Up"/>
</form></div>
<div class="right-box">
<span class="SignInWith">Sign In With<br/>Social Network</span>
<button class="social-fb" onclick="window.location = '<?php echo $FloginURL ?>';">Login
With Facebook</button> </body> </html>
----------------------------------------------------------------------------------------------------------------------
Fconnfig.php // Facebook config
<?php
require_once "Facebook/autoload.php";
$FB = new FacebookFacebook([
'app_id' => '193686724665067',
'app_secret' => '7b3a1ca8596b1c1edf3509ee8c46bfeb',
'default_graph_version' => 'v2.10'
]); $helper = $FB->getRedirectLoginHelper(); ?>
----------------------------------------------------------------------------------------------------------------------
fCallback.php // Facebook callback
<?php
require_once "Fconfig.php";
header('Location: https://056e1c1a.ngrok.io/test/logout.php');
exit();
?
Appendix-II (Sample Output)
Welcome Landing page and users will get a short description of what is Dpilot
Landing Page with a quote
Landing Page: Steps to use Dpilot
Landing Page : What we offer extra
Landing Page: Reviews
Landing Page: Above is the Contact form and providing follow us option
Login Screen: Users can Login using the username and password set by users only.
Signup Screen: Users can sign through Google account i.e. Gmail and Facebook
Password Recovery Screen
Dashboard: Front screen where users can select the files, filling up the credentials with a message.
Uploading screen with a status bar
Confirmation of the file send
Viewing and downloading screen
Mail received by the receiver through Dpilot webapp
Fig 25: Storage of files on the Amazon cloud
Database of the uploading files by the users of Dpilot Webapp
Downloading the files in zip format
Extracting the files
Bot: BOT is the additional feature of Dpilot name as “CabinCrew”.
Bot: Providing a user-friendly environment.
Ad

Recommended

Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
MongoDB
 
Silex meets SOAP & REST
Silex meets SOAP & REST
Hugo Hamon
 
Node js mongodriver
Node js mongodriver
christkv
 
Custom deployments with sbt-native-packager
Custom deployments with sbt-native-packager
GaryCoady
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Stripe
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
MongoDB
 
Jggug 2010 330 Grails 1.3 観察
Jggug 2010 330 Grails 1.3 観察
Tsuyoshi Yamamoto
 
Angular&node js upload file
Angular&node js upload file
Hu Kenneth
 
Perl Web Client
Perl Web Client
Flavio Poletti
 
連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」
matuura_core
 
Nubilus Perl
Nubilus Perl
Flavio Poletti
 
dotCloud and go
dotCloud and go
Flavio Poletti
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
XSolve
 
Say It With Javascript
Say It With Javascript
Giovanni Scerra ☃
 
Rxjs marble-testing
Rxjs marble-testing
Christoffer Noring
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQL
Peter Eisentraut
 
Rxjs vienna
Rxjs vienna
Christoffer Noring
 
V8
V8
changehee lee
 
Utilizing Powerful Extensions for Analytics and Operations
Utilizing Powerful Extensions for Analytics and Operations
Neo4j
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading example
Christopher Curtin
 
Event sourcing w PHP (by Piotr Kacała)
Event sourcing w PHP (by Piotr Kacała)
GOG.com dev team
 
Why realm?
Why realm?
Leonardo Taehwan Kim
 
The Ring programming language version 1.7 book - Part 16 of 196
The Ring programming language version 1.7 book - Part 16 of 196
Mahmoud Samir Fayed
 
Rust ⇋ JavaScript
Rust ⇋ JavaScript
Ingvar Stepanyan
 
Rxjs swetugg
Rxjs swetugg
Christoffer Noring
 
Heroku pop-behind-the-sense
Heroku pop-behind-the-sense
Ben Lin
 
EuroPython 2015 - Storing files for the web is not as straightforward as you ...
EuroPython 2015 - Storing files for the web is not as straightforward as you ...
Alessandro Molina
 
PyConFR 2014 - DEPOT, Story of a file.write() gone wrong
PyConFR 2014 - DEPOT, Story of a file.write() gone wrong
Alessandro Molina
 
Expressを使ってみた
Expressを使ってみた
Atsuhiro Takiguchi
 

More Related Content

What's hot (18)

Perl Web Client
Perl Web Client
Flavio Poletti
 
連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」
matuura_core
 
Nubilus Perl
Nubilus Perl
Flavio Poletti
 
dotCloud and go
dotCloud and go
Flavio Poletti
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
XSolve
 
Say It With Javascript
Say It With Javascript
Giovanni Scerra ☃
 
Rxjs marble-testing
Rxjs marble-testing
Christoffer Noring
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQL
Peter Eisentraut
 
Rxjs vienna
Rxjs vienna
Christoffer Noring
 
V8
V8
changehee lee
 
Utilizing Powerful Extensions for Analytics and Operations
Utilizing Powerful Extensions for Analytics and Operations
Neo4j
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading example
Christopher Curtin
 
Event sourcing w PHP (by Piotr Kacała)
Event sourcing w PHP (by Piotr Kacała)
GOG.com dev team
 
Why realm?
Why realm?
Leonardo Taehwan Kim
 
The Ring programming language version 1.7 book - Part 16 of 196
The Ring programming language version 1.7 book - Part 16 of 196
Mahmoud Samir Fayed
 
Rust ⇋ JavaScript
Rust ⇋ JavaScript
Ingvar Stepanyan
 
Rxjs swetugg
Rxjs swetugg
Christoffer Noring
 
連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」
matuura_core
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
XSolve
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQL
Peter Eisentraut
 
Utilizing Powerful Extensions for Analytics and Operations
Utilizing Powerful Extensions for Analytics and Operations
Neo4j
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading example
Christopher Curtin
 
Event sourcing w PHP (by Piotr Kacała)
Event sourcing w PHP (by Piotr Kacała)
GOG.com dev team
 
The Ring programming language version 1.7 book - Part 16 of 196
The Ring programming language version 1.7 book - Part 16 of 196
Mahmoud Samir Fayed
 

Similar to Source Code for Dpilot (15)

Heroku pop-behind-the-sense
Heroku pop-behind-the-sense
Ben Lin
 
EuroPython 2015 - Storing files for the web is not as straightforward as you ...
EuroPython 2015 - Storing files for the web is not as straightforward as you ...
Alessandro Molina
 
PyConFR 2014 - DEPOT, Story of a file.write() gone wrong
PyConFR 2014 - DEPOT, Story of a file.write() gone wrong
Alessandro Molina
 
Expressを使ってみた
Expressを使ってみた
Atsuhiro Takiguchi
 
Writing RESTful web services using Node.js
Writing RESTful web services using Node.js
FDConf
 
apidays LIVE New York - API Code First vs Design First by Phil Sturgeon
apidays LIVE New York - API Code First vs Design First by Phil Sturgeon
apidays
 
File upload using multer in node.js and express.js [2021 tutorial]
File upload using multer in node.js and express.js [2021 tutorial]
Katy Slemon
 
Rapid prototyping and easy testing with ember cli mirage
Rapid prototyping and easy testing with ember cli mirage
Krzysztof Bialek
 
"#Microfrontends #LowConnectivity #AsianMarket", Maxim Demidenko
"#Microfrontends #LowConnectivity #AsianMarket", Maxim Demidenko
Fwdays
 
ES6(ES2015) is beautiful
ES6(ES2015) is beautiful
monikagupta18jan
 
Relaxing With CouchDB
Relaxing With CouchDB
leinweber
 
development, ruby, conferences, frameworks, ruby on rails, confreaks, actsasc...
development, ruby, conferences, frameworks, ruby on rails, confreaks, actsasc...
ActsAsCon
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
Igor Bronovskyy
 
StirTrek 2018 - Rapid API Development with Sails
StirTrek 2018 - Rapid API Development with Sails
Justin James
 
Create Rest API in Nodejs
Create Rest API in Nodejs
Irfan Maulana
 
Heroku pop-behind-the-sense
Heroku pop-behind-the-sense
Ben Lin
 
EuroPython 2015 - Storing files for the web is not as straightforward as you ...
EuroPython 2015 - Storing files for the web is not as straightforward as you ...
Alessandro Molina
 
PyConFR 2014 - DEPOT, Story of a file.write() gone wrong
PyConFR 2014 - DEPOT, Story of a file.write() gone wrong
Alessandro Molina
 
Writing RESTful web services using Node.js
Writing RESTful web services using Node.js
FDConf
 
apidays LIVE New York - API Code First vs Design First by Phil Sturgeon
apidays LIVE New York - API Code First vs Design First by Phil Sturgeon
apidays
 
File upload using multer in node.js and express.js [2021 tutorial]
File upload using multer in node.js and express.js [2021 tutorial]
Katy Slemon
 
Rapid prototyping and easy testing with ember cli mirage
Rapid prototyping and easy testing with ember cli mirage
Krzysztof Bialek
 
"#Microfrontends #LowConnectivity #AsianMarket", Maxim Demidenko
"#Microfrontends #LowConnectivity #AsianMarket", Maxim Demidenko
Fwdays
 
Relaxing With CouchDB
Relaxing With CouchDB
leinweber
 
development, ruby, conferences, frameworks, ruby on rails, confreaks, actsasc...
development, ruby, conferences, frameworks, ruby on rails, confreaks, actsasc...
ActsAsCon
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
Igor Bronovskyy
 
StirTrek 2018 - Rapid API Development with Sails
StirTrek 2018 - Rapid API Development with Sails
Justin James
 
Create Rest API in Nodejs
Create Rest API in Nodejs
Irfan Maulana
 
Ad

Recently uploaded (20)

COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
The basics of hydrogenation of co2 reaction
The basics of hydrogenation of co2 reaction
kumarrahul230759
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
Cadastral Maps
Cadastral Maps
Google
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
The basics of hydrogenation of co2 reaction
The basics of hydrogenation of co2 reaction
kumarrahul230759
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
Cadastral Maps
Cadastral Maps
Google
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Ad

Source Code for Dpilot

  • 1. Appendix-I (Program Code) File.js // Collecting data from the users (sender) import _ from 'lodash' class File { constructor(app) { this.app = app; this.model = { name: null, originalName: null, mimeType: null, size: null, etag: null, created: new Date(), } } initWithObject(object){ this.model.name = _.get(object, 'key'); this.model.originalName = _.get(object, 'originalname'); this.model.mimeType = _.get(object, 'mimetype'); this.model.size = _.get(object, 'size'); this.model.etag= _.get(object, 'etag'); this.model.created = new Date(); return this; } toJSON(){ return this.model; } save(callback) { const db = this.app.get('db'); db.collection('files').insertOne(this.model, (err, result) => { return callback(err, result); });} } export default File; -------------------------------------------------------------------------------------------------------------------- Archiver.js // Download all the files as a zip file import archiver from 'archiver' import _ from 'lodash' import path from 'path' import S3 from './s3' export default class FileArchiver{
  • 2. constructor(app, files = [], response){ this.app = app; this.files = files; this.response = response; } download(){ const app = this.app; const files = this.files; //const uploadDir = app.get('storageDir'); const zip = archiver('zip'); const response = this.response; response.attachment('download.zip'); zip.pipe(response); const s3Downloader = new S3(app, response); _.each(files, (file) => { const fileObject = s3Downloader.getObject(file); zip.append(fileObject, {name: _.get(file, 'originalName')}); }); zip.finalize(); return this; }} -------------------------------------------------------------------------------------------------------------------- Config.js // Sending Notification Alert through Email import {accessKeyId,secretAccessKey} from './s3-config.json' export const smtp = { host: 'smtp.sendgrid.net', port: 587, secure: false, // true for 465, false for other ports auth: { user: 'apikey', // generated ethereal user pass: 'SG.HXUZZppORTSHzQuQwC-5EA.OgpSmEJnvXwo2qBGX- ujkk7OqB1hSw2Kwlxal9abLUI' // generated ethereal password }} export const url = 'http://localhost:3001'; export const s3Config = { accessKeyId: 'AKIAI7PBFWK7ZNHYIGQA', secretAccessKey: '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'} export const S3Region = 'us-east-1' export const s3Bucket = 'fileapp0' -------------------------------------------------------------------------------------------------------------------- Database.js // Connecting the database through Mongodb import {MongoClient} from 'mongodb'; const url ='mongodb://localhost:27017/fileapp'; export const connect=(callback)=>{ MongoClient.connect(url, (err, db) => {
  • 3. return callback(err, db); }); }; -------------------------------------------------------------------------------------------------------------------- Index.js // Monitor for the file present in package.JSON import http from 'http'; import express from 'express'; import {connect} from "./database"; import AppRouter from './router' import multer from 'multer' import path from 'path'; import nodemailer from 'nodemailer'; import {smtp, s3Config, S3Region, s3Bucket} from './config' // Amazon S3 Setup import AWS from 'aws-sdk' import multerS3 from 'multer-s3' AWS.config.update(s3Config); AWS.config.region = S3Region ; const s3 = new AWS.S3(); // setup Email let email = nodemailer.createTransport(smtp); //File Storage Config const storageDir = path.join(__dirname,'..', 'storage'); // const upload = multer({ storage: storageConfig }) const upload = multer({ storage: multerS3({ s3: s3, bucket: s3Bucket, metadata: function (req, file, cb) { cb(null, {fieldName: file.fieldname}); }, key: function (req, file, cb) { const filename = `${Date.now().toString()}-${file.originalname}`; cb(null, filename) } }) }) //End File Storage Config const PORT = 3000; const app = express(); app.server = http.createServer(app); app.use(morgan('dev'));
  • 4. app.use(cors({ exposedHeaders: "*"})); app.set('root', __dirname); app.set('storageDir',storageDir); app.upload = upload; app.email = email; app.s3 = s3; //Connect to database connect((err,db)=> { if (err) { console.log("An error connecting to the database"); throw(err); } app.set('db',db); //init router new AppRouter(app); app.server.listen(process.env.PORT || PORT, () => { console.log(`App is running on port ${app.server.address().port}`); }); }); export default app; -------------------------------------------------------------------------------------------------------------------- Router.js // JS library that provide an API for handling routes import {version} from '../package.json' import path from 'path' import _ from 'lodash' import {ObjectID} from 'mongodb' import File from './models/file' import Post from './models/post' import FileArchiver from './archiver' import Email from './email' import S3 from './s3' class AppRouter { constructor(app) { this.app = app; this.setupRouters(); } setupRouters() { const app = this.app; const uploadDir = app.get('storageDir');
  • 5. const upload = app.upload; const db = app.get('db'); //root routing app.get('/', (req, res, next) => { return res.status(200).json({ version: version }); }); //Upload Routing app.post('/api/upload', upload.array('files'), (req, res, next) => { const files = _.get(req, 'files', []); console.log("files objects from s3 multer", files); let fileModels = []; _.each(files, (fileObject) => { const newFile = new File(app).initWithObject(fileObject).toJSON(); fileModels.push(newFile); }); if (fileModels.length) { //check for empty file db.collection('files').insertMany(fileModels, (err, result) => { if (err) { return res.status(503).json({ error: { message: "Unable to save your file", } }); } let post = new Post(app).initWithObject({ from: _.get(req,'body.from'), to: _.get(req,'body.to'), message: _.get(req,'body.message'), phone: _.get(req,'body.phone'), files: result.insertedIds, }).toJSON(); //let save post objects to posts collection. db.collection('posts').insertOne(post,(err,result)=>{ if(err){ return res.status(503).json({error:{message:"Your upload could not be saved"}}); } //implement email sending to user with download link. //send email const sendEmail = new Email(app).sendDownloadLink(post, (err, info) => { });
  • 6. // callback to react app with post detail. return res.json(post); }); }); } else { return res.status(503).json({ error: {message: "files upload is required"} }); } }); // Download file from S3 service const file = _.get(result, '[0]'); const downloader = new S3(app, res); // return downloader.download(file); Proxy download from s3 service // Download Directly from S3 const downloadUrl = downloader.getDownloadUrl(file); return res.redirect(downloadUrl); }); }); //routing for post detail/api/posts/:id app.get('/api/posts/:id',(req,res,next)=>{ const postId=_.get(req,'params.id'); this.getPostById(postId, (err, result) => { if(err){ return res.status(404).json({error:{message: 'File not found.'}}); } return res.json(result); }) }); // Routing download zip files. app.get('/api/posts/:id/download', (req, res, next) => { const id = _.get(req, 'params.id', null); this.getPostById(id, (err, result) => { if (err) { return res.status(404).json({error: {message: 'File not found.'}}); } const files = _.get(result, 'files', []); const archiver = new FileArchiver(app, files, res).download(); return archiver; }) }); } getPostById(id, callback = () => {}){ const app= this.app; const db = app.get('db'); let postObjectId=null; try{ postObjectId=new ObjectID(id); }
  • 7. catch (err){ return callback(err, null); } db.collection('posts').find({_id:postObjectId}).limit(1).toArray((err,results)=>{ let result =_.get(results,'[0]'); if(err || !result){ return callback(err ? err: new Error("File not found")); } const fileIds=_.get(result,'files',[]); db.collection('files').find({_id:{$in: fileIds}}).toArray((err,files)=>{ if(err || !files || !files.length){ return callback(err ? err: new Error("File not found")); } result.files=files; return callback(null, result); }); }) }} export default AppRouter; ---------------------------------------------------------------------------------------------------------------------- S3.js // Connecting the S3 with Dpliot for Cloud Application import _ from 'lodash' import {s3Bucket} from './config' export default class S3{ constructor(app, response){ this.app = app; this.response = response; } getObject(file){ const s3 = this.app.s3; const options ={ Bucket: s3Bucket, Key: _.get(file, 'name') }; return s3.getObject(options).createReadStream(); } download(file){ const s3 = this.app.s3; const response = this.response; // get Object from S3 service const filename = _.get(file, 'originalName'); response.attachment(filename); const options ={
  • 8. Bucket: s3Bucket, Key: _.get(file, 'name') }; const fileObject = s3.getObject(options).createReadStream(); fileObject.pipe(response); } getDownloadUrl(file){ const s3 = this.app.s3; const options ={ Bucket: s3Bucket, Key: _.get(file, 'name'), Expires: 3600, // one hour expires. }; const url = s3.getSignedUrl('getObject', options); return url; }} -------------------------------------------------------------------------------------------------------------------- Header.js // Designing Dashboard through React import React,{Component} from 'react' class Header extends Component { render() { return ( <div className={'app-header'}> <div className={'app-site-info'}> <h1><i className={'icon-paper-plane'} /> Dpilot</h1> <div className={'site-title'}>SHARE YOUR FILE.</div> <div className={'site-slogan'}>SECURE ! SAFE ! FREE !</div> </div> </div>) }}export default Header; ---------------------------------------------------------------------------------------------------------------------- Home-form.js // Container collecting the credentials of the user as a form import React, {Component} from 'react' import _ from 'lodash' import classNames from 'classnames' import {upload} from '../helpers/upload' import PropTypes from 'prop-types' class HomeForm extends Component{ constructor(props){ super(props); this.state = { form: { files: [], to: '', from: '', phone: '', message: '' }, errors:{
  • 9. to: null, from: null, phone:null, message: null, files: null, } }; this._onTextChange = this._onTextChange.bind(this); this._onSubmit = this._onSubmit.bind(this); this._formValidation = this._formValidation.bind(this); this._onFileAdded = this._onFileAdded.bind(this); this._onFileRemove = this._onFileRemove.bind(this) } _isEmail(emailAddress) { const emailRegex = /^(([^<>()[].,;:s@"]+(.[^<>()[].,;:s@"]+)*)|(".+"))@(([^<>()[].,;:s@"]+.)+[^<>()[].,;:s@"]{2, })$/i; return emailRegex.test(emailAddress); } _isPhone(phoneNumber){ const phoneRegex = /^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/; return phoneRegex.test(phoneNumber); } _formValidation(fields = [], callback = () => {}){ let {form, errors} = this.state; _onSubmit(event){ event.preventDefault(); this._formValidation(['from', 'to','phone','files'], (isValid) => { if(isValid){ //the form is valid and ready to submit. const data = this.state.form; if(this.props.onUploadBegin){ this.props.onUploadBegin(data); } upload(data, (event) => { if(this.props.onUploadEvent){ this.props.onUploadEvent(event); } }) } }); } _onTextChange(event){ let{form} = this.state; const fieldName = event.target.name; const fieldValue = event.target.value; form[fieldName] = fieldValue;
  • 10. this.setState({form: form}); } render(){ const {form,errors}= this.state; const {files} = form; return( <div className={'app-card'}> <form onSubmit={this._onSubmit}> <div className={'app-card-header'}> <div className={'app-card-header-inner'}> { files.length ? <div className={'app-files-selected'}> { files.map((file, index) => { return ( <div key= {index} className={'app-files-selected-item'}> <div className={'filename'}>{file.name}</div> <div className={'file-action'}> <button onClick={() => this._onFileRemove(index)} type={'button'} className={'app-file-remove'}>X </button> </div> </div> ) }) } </div> : null } <div className={classNames('app-file-select-zone',{'error':_.get(errors,'files')})}> <label htmlFor={'input-file'}> <input onChange={this._onFileAdded} id={'input-file'} type="file" multiple={true} /> { files.length ? <span className={'app-upload-description text- uppercase'}>Add more files</span> : <span> <span className={'app-upload-icon'}><i className={'icon-picture- o'} /> </span> <span className={'app-upload-description'}>Drag and drop your files here.</span> </span> } </label> </div> </div> </div> <div className={classNames('app-form-item',{'error':_.get(errors,'phone')})}> <label htmlFor={'phone'}>Receiver's Contact Number</label> <input value={_.get(form, 'phone' )} onChange={this._onTextChange} name={'phone'} placeholder={_.get(errors,'phone') ? _.get(errors,'phone'):'Your Contact Number'} type={'text'} id={'phone'} maxLength={'10'}/> </div> <div className={'app-form-item'}> <label htmlFor={'message'}>Message</label> <textarea value= {_.get(form, 'message', '')} onChange={this._onTextChange} name={'message'} placeholder={'Add a note (optional)'} id={'message'}/>
  • 11. </div> <div className={'app-form-actions'}> <button type={'submit'} className={'app-button primary'}>Send</button> </div> </div> </div> </form> </div> ) }} HomeForm.propTypes = { onUploadBegin: PropTypes.func, onUploadEvent: PropTypes.func }; export default HomeForm; -------------------------------------------------------------------------------------------------------------------- Home-upload-sent.js // Container confirming the files sent to the receiver import React,{Component} from 'react' import PropTypes from 'prop-types' import _ from 'lodash' import {history} from "../history"; class HomeUploadSent extends Component{ constructor(props){ super(props); } render(){ const {data}=this.props; console.log( "Data",data); const to=_.get(data,'to'); const postId=_.get(data,'_id'); return( <div className={'app-card app-card-upload-sent'}> <div className={'app-card-content'}> <div className={'app-card-content-inner'}> <div className={'app-home-uploading'}> <div className={'app-home-upload-sent-icon'}> <i className={'icon-paper-plane'} /> </div> <div className={'app-upload-sent-message app-text-center'}> <h2>Files Sent! </h2> <p>We have sent an email to {to} with a download link.The link will expire in 30 days</p> </div> <div className={'app-upload-sent-actions app-form-actions'}> <button onClick={()=>{
  • 12. history.push( `/share/${postId}`) }} className={'app-button primary'} type={'button '}>View File </button> <button onClick={()=>{ if(this.props.onSendAnotherFile){ this.props.onSendAnotherFile(true); } }} className={'app-button'} type={'button'}> Send Another File </button> </div> </div> </div> </div> </div> ) }} HomeUploadSent.propTypes={ data: PropTypes.object, onSendAnotherFile: PropTypes.func } export default HomeUploadSent; ---------------------------------------------------------------------------------------------------------------------- Upload.js //Container confirming the status of the uploaded files import axios from 'axios' import {apiUrl} from "../config"; import _ from 'lodash' export const upload = (form, callback = () => {}) => { const url = `${apiUrl}/upload`; let files = _.get(form, 'files', []); let data = new FormData(); _.each(files, (file) => { data.append('files', file); }); // upload successful. axios.post(url, data, config).then((response) => { return callback({ type: 'success', payload: response.data }) }).catch((err) => { return callback({ type: 'error', payload: err }) }); }; ---------------------------------------------------------------------------------------------------------------------- GoogleConfig.php // Checking the Google Configuration of the user. <?php session_start(); require_once "GoogleAPI/vendor/autoload.php"; $gClient = new Google_Client();
  • 13. $gClient->setClientId("643208222747- snjv039ocno2kjcfvpfebvorn6ojovnk.apps.googleusercontent.com"); $gClient->setClientSecret("SHtRqIGKQFhQLY1A78g2hzdu"); $gClient->setApplicationName("CPI Login Tutorial"); $gClient->setRedirectUri("http://localhost/test/g-callback.php"); $gClient->addScope("https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email");?> ---------------------------------------------------------------------------------------------------------------------- Connect.php // Connect to DB <?php $connection=mysqli_connect('localhost','root',''); if(!$connection){ echo"failed";} $dbSelect=mysqli_select_db($connection,'demo'); if(!$dbSelect){ echo"failed selection"; die(mysqli_error($connection)); }?> ---------------------------------------------------------------------------------------------------------------------- gCallback.php //Google Callback <?php require_once "config.php"; if (isset($_SESSION['access_token'])) $gClient->setAccessToken($_SESSION['access_token']); else if (isset($_GET['code'])) { $token = $gClient->fetchAccessTokenWithAuthCode($_GET['code']); $_SESSION['access_token'] = $token; } else { header('Location: index11.php'); exit(); } $oAuth = new Google_Service_Oauth2($gClient); $userData = $oAuth->userinfo_v2_me->get(); $_SESSION['id'] = $userData['id']; $_SESSION['email'] = $userData['email']; $_SESSION['gender'] = $userData['gender']; $_SESSION['picture'] = $userData['picture']; $_SESSION['familyName'] = $userData['familyName']; $_SESSION['givenName'] = $userData['givenName']; header('Location: http://www.facebook.com');
  • 14. exit(); ?> ---------------------------------------------------------------------------------------------------------------------- Login.php // Accepting the credentials for logging in of the user. <?php if(isset($_POST["submit"])){ require_once('connect.php'); if(isset($_POST) & !empty($_POST)){ $uname=$_POST['username']; $password=$_POST['password']; $sql="SELECT * FROM usermanagement WHERE username='".$uname."' AND password='".$password."'"; $result=mysqli_query($connection,$sql); if(mysqli_num_rows($result)==1){ header("Location: http://www.google.com"); exit(); }else { $message="we r done "; } } } ?> <?php if(isset($_POST["recover"])){ require_once('connect.php'); if(isset($_POST) & !empty($_POST)){ $email=$_POST["eemail"]; $data="SELECT * FROM usermanagement WHERE email='".$email."'"; $rresult=mysqli_query($connection,$data); if(mysqli_num_rows($rresult)==1){ $row= mysqli_fetch_assoc($rresult); require 'phpmailer/PHPMailerAutoload.php'; $mail = new PHPMailer(); $mail->isSMTP(); $mail->Host = "smtp.gmail.com"; $mail->SMTPSecure = "ssl"; $mail->Port = 465; $mail->SMTPAuth = true; $mail->Username = '[email protected]'; $mail->Password = 'Deep@21081998'; $mail->setFrom('[email protected]', 'Deepanshu Shamra');
  • 15. $mail->addAddress( $row["email"]); $mail->Subject = 'Password:Dpilot'; $mail->Body = "Your Password to access Dpilot is:" .$row["password"]; if ($mail->send()) $mess= "A mail containting password of your account is sent to you."; else $mess="something went wrong"; }else{ $mess="Please enter the registered email"; }}} ?> <html> <head> <title>Dpilot Login/Signup </title> <link rel="stylesheet" type="text/css" href="style.css"> <link rel="stylesheet" type="text/css" href="fp.css"> <link rel="stylesheet" type="text/css" href="recover_style.css"> <body> <div class="loginbox"> <img src="sampleD.png" class="avatar"> <h1 > Login Here</h1> <form method="post" action="#"> <p>Username</p> <input type="text" name="username" placeholder ="Enter Username" value="<?php if(isset($uname) and!empty($uname)) {echo $uname;}?>" required> <p>Password</p> <input type="password" name="password" placeholder="Enter Password"value="" required > <div> <?php if(isset($message) and!empty($message)){echo $message;} ?> </div> <input type="submit" name="submit" value="Login"> <a href="dex.php" class="link">Dont Have an Account?</a><br> <a class="link1" href="#modal" class="modal-open" id="modal-open">Lost Your Password?</a></form> <div class="modal" id="modal"> <div class="modal-content"> <a href="" class="modal-close">&times;</a> <div class="lloginbox"> <img src="sampleD.png" class="aavatar"> <h1 > Password Recovery</h1> <form action="index11.php#modal" method="post"> <p id="ww">Registered Email</p> <input type="text" name="eemail" placeholder="Enter Email"> <input type="submit" name="recover" value="Recover Password"> <div> <?php if(isset($mess) and!empty($mess)){echo $mess;} ?> </div> </form> </div> </div> </div> </div> </body> </head> </html> ---------------------------------------------------------------------------------------------------------------------- Signup.php // Accepting the credentials for signing up of the new user.
  • 16. <?php require_once('connect.php'); if(isset($_POST) & !empty($_POST)){ $username= $_POST['username']; $email= $_POST['email']; $password= $_POST['password']; $repassword= $_POST['password2']; if(!$username OR !$email OR !$password) { $message="Please Fill All the details";} else{ if($password==$repassword){ {$sql="INSERT INTO usermanagement (username,email,password) VALUES ('$username','$email','$password')"; $result=mysqli_query($connection,$sql); header("Location: http://www.google.com"); } if($result){ $message="success"; }else $message="Username already exist";} else $message="Password doesnot match"; }}?> <?php require_once "config.php"; if (isset($_SESSION['access_token'])) { header('Location: https://056e1c1a.ngrok.io/test/dex.php'); exit(); } $loginURL = $gClient->createAuthUrl();?> <?php require_once "Fconfig.php"; if (isset($_SESSION['access_token'])) { header(' Location: https://056e1c1a.ngrok.io/test/logout.php'); exit(); } $redirectURL = " https://056e1c1a.ngrok.io/test/f-callback.php"; $permissions = ['email']; $FloginURL = $helper->getLoginUrl($redirectURL, $permissions); ?> <html> <head> <title> Dpilot Signup </title>
  • 17. <link rel="stylesheet" type="text/css" href="styl.css"> </head> <body> <div id="login-box"> <div id="left-box"> <form class="form-signin" method="post" > <h1>Sign Up</h1> <div id="al"> <?php if(isset($message) and!empty($message)){echo $message;} ?> </div> <input type="text" name="username" placeholder="Username" value="<?php if(isset($result) and!empty($result)){if($result) echo "";}else if(isset($username) and!empty($username)) {echo $username;} ?>" required/> <input type="text" name="email" placeholder="Email" value="<?php if(isset($result) and!empty($result)){if($result) echo "";}else if(isset($email) and!empty($email)) {echo $email;} ?>" required/> <input type="password" name="password" placeholder="Password" /> <input type="password" name="password2" placeholder="Retype Password"/> <input type="submit" name="signup-button" value="Sign Up"/> </form></div> <div class="right-box"> <span class="SignInWith">Sign In With<br/>Social Network</span> <button class="social-fb" onclick="window.location = '<?php echo $FloginURL ?>';">Login With Facebook</button> </body> </html> ---------------------------------------------------------------------------------------------------------------------- Fconnfig.php // Facebook config <?php require_once "Facebook/autoload.php"; $FB = new FacebookFacebook([ 'app_id' => '193686724665067', 'app_secret' => '7b3a1ca8596b1c1edf3509ee8c46bfeb', 'default_graph_version' => 'v2.10' ]); $helper = $FB->getRedirectLoginHelper(); ?> ---------------------------------------------------------------------------------------------------------------------- fCallback.php // Facebook callback <?php require_once "Fconfig.php"; header('Location: https://056e1c1a.ngrok.io/test/logout.php'); exit(); ?
  • 18. Appendix-II (Sample Output) Welcome Landing page and users will get a short description of what is Dpilot Landing Page with a quote Landing Page: Steps to use Dpilot
  • 19. Landing Page : What we offer extra Landing Page: Reviews Landing Page: Above is the Contact form and providing follow us option
  • 20. Login Screen: Users can Login using the username and password set by users only. Signup Screen: Users can sign through Google account i.e. Gmail and Facebook Password Recovery Screen
  • 21. Dashboard: Front screen where users can select the files, filling up the credentials with a message. Uploading screen with a status bar Confirmation of the file send
  • 22. Viewing and downloading screen Mail received by the receiver through Dpilot webapp Fig 25: Storage of files on the Amazon cloud
  • 23. Database of the uploading files by the users of Dpilot Webapp Downloading the files in zip format Extracting the files
  • 24. Bot: BOT is the additional feature of Dpilot name as “CabinCrew”. Bot: Providing a user-friendly environment.