Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nateowami committed Jun 21, 2015
0 parents commit b268a76
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Nathaniel Paulus

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
httptooth.js
============

Transfer files and text between devices with HTTP.

This project was born out of frustration with trying to email Chinese characters between devices that didn't support Bluetooth. Such a small task should have a simple way to do it.

Usage
=====

#### Installation

Start with `git clone https://github.com/Nateowami/httptooth.js.git`. Then `cd httptooth.js` and run `npm install` (may need `sudo`).

#### Running

To run it, type `node .`. By default it will run on port 3000. If you want to specify a different port, use the `PORT` environment variable (e.g. `PORT=8080 node .`). Now navigate to `localhost:3000` (or whatever port you launched it on) and you'll be given further usage instructions. Note that to access httptooth.js from a device that isn't running it, you'll need to specify the IP address of your computer, rather than `localhost`.

#### Uploading

Start by clicking the upload tab. On the next page you'll be able to upload multiple files (in browsers that support multiple file selections) or paste text in the text box. You can upload text and files at the same time. Click "Upload Data" at the bottom of the page, and you'll be shown all uploaded data, as well as a confirmation of what was uploaded.

#### Downloading

To download, navigate to the download tab. Copy text from this page or click a file name to download it.

#### Deleting

Navigate to `public/uploads` in the directory where httptooth.js is running, and delete the files from there. To delete text, just delete `uploaded_text.json` from the `public` directory.
122 changes: 122 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Setup Express
var express = require("express");
var app = express();
app.use(require("multer")(
{
dest: __dirname + '/public/uploads',
rename: function(fieldname, filename) {
return filename;
}
}));
app.use(require("body-parser").json());

var fs = require("fs");
// Declare an array for uploaded text
var uploadedText = [];

// Configure views
app.set("views", "./views");
app.set("view engine", "jade");

// Server static files
app.use("/assets", express.static("public/assets"));

// GET pages
app.get("/", function(req, res){
res.render("index");
});

app.get("/upload", function(req, res){
res.render("upload");
});

app.get("/download", function(req, res){
// Find uploaded files
listUploads(function(files){
res.render("download", {text: uploadedText, files: files});
});
});

app.get("/uploads/:file", function(req, res){
res.download(__dirname + "/public/uploads/" + req.params.file);
});

// POST data
app.post("/upload", function(req, res){
// Files are automatically saved by multer
// Save text
if(req.body.text) addText(req.body.text);
//return res.send(JSON.stringify(req.body.text));
// See whether files or text was uploaded
var text = !!req.body.text, files = !!req.files.files;
var msg = "";
// If something was uploaded
if(text || files){
if(text && files) msg = "Text and files were";
else if(text && !files) msg = "Text was";
else msg = "Files were";
msg += " uploaded successfully."
}
else msg = "Nothing was uploaded.";

listUploads(function(list){
res.render("download",
{
msg: msg,
text: uploadedText,
files: list
});
});
});

// Connect to the specified port
var server = app.listen(process.env.PORT || 3000, function(){
// Say where we're listening for requests
console.log(
"httptooth.js listening at http://%s:%s",
server.address().address,
server.address().port
);
});

// Saves a new piece of uploaded text
function addText(text){
uploadedText.push(text);
fs.writeFile(__dirname + "/public/uploaded_text.json",
JSON.stringify(uploadedText),
function(err) {if(err) throw err;});
}

fs.readFile(__dirname + "/public/uploaded_text.json",
"utf8",
function(err, data){
if(err && err.code != "ENOENT") throw err;
else if (data) uploadedText = JSON.parse(data);
});

// Calls cb with an array of files in public/uploads
// Each element has a name and url
function listUploads(cb){
var list = [];
var dir = __dirname + "/public/uploads/";
// List files and dirs
fs.readdir(dir, function(err, files){
// The dir doesn't have to exist
if(err && err.code == "ENOENT") return [];
else if(err) throw err;
// Check each to see if it's a file
for(var i = 0; i < files.length; i++){
// If it's a file, add it to the list
if(!fs.statSync(dir + files[i]).isDirectory()){
list.push(
{
name: files[i],
url: "/uploads/"+files[i]
}
);
}
}
// Send the list to the callback
cb(list);
});
}
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "httptooth.js",
"version": "1.0.0",
"description": "Transfer files & text between devices with HTTP",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/Nateowami/httptooth.js"
},
"keywords": [
"networking",
"files",
"devices",
"transfer"
],
"author": "Nathaniel Paulus",
"license": "MIT",
"bugs": {
"url": "https://github.com/Nateowami/httptooth.js/issues"
},
"homepage": "https://github.com/Nateowami/httptooth.js",
"dependencies": {
"body-parser": "^1.13.1",
"express": "^4.12.4",
"jade": "^1.11.0",
"multer": "^0.1.8"
}
}
94 changes: 94 additions & 0 deletions public/assets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* Main elements */

body {
font-family: ubuntu;
}

.content {
width: 100%;
max-width: 60em;
margin: 0 auto 0;
border: 2pt solid #e0e0e0;
background-color: #f8f8f8;
border-radius: 5pt;
word-wrap: break-word;
}

h1, h2 {
margin-left: 10pt;
}

.body li {
font-size: 14pt;
margin: 5pt;
}

p.msg {
border: 2pt dashed #e0e0e0;
padding: 3pt;
font-weight: bold;
font-size: 16pt;
}

form {
margin: 10pt;
}

form * {
margin-left: 10pt;
}

textarea {
width: 100%;
max-width: 300pt;
}

p {
margin: 20pt;
}

p.note {
font-style: italic;
}

/* Navigation */

.header ul {
width: 100%;
padding: 0;
margin: 0;
list-style: none;
display: block;
text-align: center;
}

.header li {
width: 33.334%;
padding: 0;
margin: 0;
display: inline-block;
text-align: center;
/* IE9- */
list-style: none;
}

.header li a {
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
text-decoration: none;
color: black;
font-size: 1.5em;
width: 100%;
display: inline-block;
padding: 10pt 0 10pt;
border: 2pt solid #e0e0e0;
background-color: #e0e0e0;

}

.header li a:hover {
background-color: white;
}


26 changes: 26 additions & 0 deletions views/download.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extends layout

block content
h1 Download

h2 Text

p Copy the text below to "download."
if text.length
ol
for i in text
li: p=i
else
p.note No text has been uploaded.

h2 Files

p Click a file name to download.
if files.length
ol
each file in files
li: a(href=file.url, target="_blank" download)=file.name
else
p.note No files have been uploaded.

p.note To delete files, navigate to public/uploads in the directory where httptooth.js is running, and delete the files from there. To delete text, just delete uploaded_text.json from the public directory.
14 changes: 14 additions & 0 deletions views/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extends layout

block content
h1 Get started

ol
li Click the upload tab
li On the page that loads, select files or paste text
li Click "Upload"
li Visit this page on the device you want to send files to
li Click the download tab
li Click the download button beside each file, or copy text

p.note You can also upload files from the device you have the server running on by pasting them in the public/uploads directory. Uploads will appear in that directory, so you can copy them form there if you like.
18 changes: 18 additions & 0 deletions views/layout.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
doctype html
html(lang="en")
head
title Transfer files & text with httptooth.js
link(rel="stylesheet" href="/assets/style.css")
body
.content
.header
ul
li: a(href="/") Home
li: a(href="/upload") Upload
li: a(href="/download") Download
.body
if(msg)
p.msg=msg
block content

p Powered by <a href="https://github.com/Nateowami/httptooth.js">httptooth.js</a> v1.0, written by <a href="https://github.com/Nateowami">Nathaniel Paulus</a>, licensed with the <a href="http://opensource.org/licenses/MIT">MIT</a>, and awaiting your contributions on <a href="https://github.com/Nateowami/httptooth.js">GitHub</a>.
19 changes: 19 additions & 0 deletions views/upload.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extends layout

block content
h1 Upload

p Select files or paste text into the textbox (or both). Then click the upload button.

form(action="/upload" method="post" enctype="multipart/form-data")

h2 Text upload
textarea(rows="10", name="text")

h2 File upload
p Warning: If you upload two files with the same name, the second will overwrite the first!
input(type="file" name="files" multiple)

h2 Upload it

button(type="submit") Upload Data

0 comments on commit b268a76

Please sign in to comment.