Skip to content

Commit

Permalink
Use custom hash for pic pages
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Aug 21, 2011
1 parent d4073ec commit 0ddaed3
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 77 deletions.
63 changes: 32 additions & 31 deletions ircbot.coffee
Expand Up @@ -7,14 +7,13 @@ _ = require 'underscore'
_.mixin require 'underscore.string'

urlsortener = require "./urlsortener"
{ShareItem} = require "./shareitem"

config = JSON.parse fs.readFileSync "./config.json"

kue.redis.createClient = ->
client = redis.createClient(config.redis.port, config.redis.host)
if config.redis.pass?
client.auth config.redis.pass
client
conn = require "./redisconnection"
kue.redis.createClient = conn.getClient


jobs = kue.createQueue()
db = jobs.client
Expand Down Expand Up @@ -87,39 +86,41 @@ class IRCPoster
userId: (nick, deviceid) ->
"reg:#{ nick }@#{ @networkName }:#{ deviceid }"

askToRegister: (nick, data) ->
askToRegister: (share) ->
nick = share.data.nick
data = share.data
@client.say nick, "Hi!"
@client.say nick, "Somebody is trying to post a picture from
#{ data.devicename } with caption \" #{ data.caption }\" to #{ data.channel } as you."
@client.say nick, "This nickname is not registered with that device on IRCShare.com."
@client.say nick, "Somebody is trying to post a picture from
#{ data.devicename } (#{ share.getUrl() }) to #{ data.channel } as you."
@client.say nick, "This nickname is not registered with that device on IRCShare.com."
@client.say nick, "If this is you, respond to me with: \"ok #{ data.deviceid }\" without the quotes."
@client.say nick, "If this is not you, respond with: \"no #{ data.deviceid }\"."

startJobProcessor: ->
jobs.process "irc-#{ @networkName }", (job, done) =>

msg = "<#{ job.data.nick }> #{ job.data.caption } #{ config.domain }#{ urlsortener.encode job.id }"
userid = @userId(job.data.nick, job.data.deviceid)

db.get userid, (err, registered) =>
console.log "process", err, registered
if registered is "ok"
console.log "SAYING", job.data.channel
@sayAndPart job.data.channel, msg, ->
console.log "done", msg
share = new ShareItem id: job.data.shareId, config: config
share.load (err) =>
return done err if err

msg = "<#{ share.data.nick }> #{ share.data.caption } #{ share.getUrl()}"
console.log "getting user id for", share.data
userid = @userId(share.data.nick, share.data.deviceid)

db.get userid, (err, registered) =>
if registered is "ok"
console.log "SAYING", share.data.channel
@sayAndPart share.data.channel, msg, ->
console.log "done", msg
done()
share.set status: "ok"
else if registered is "no"
done()
job.data.status = "ok"
job.save()
else if registered is "no"
done()
job.data.status = "#{ job.data.nick } refused."
job.save()
return
else
done new Error "#{ userid } is not registered yet. Device #{ job.data.devicename }"
@askToRegister job.data.nick, job.data
job.data.status = "asking for confirmation"
job.save()
share.set status: "#{ share.data.nick } refused."
return
else
done new Error "#{ userid } is not registered yet. Device #{ share.data.devicename }"
@askToRegister share
share.set status: "Asking for confirmation from #{ share.data.nick }"


start: ->
Expand Down
2 changes: 1 addition & 1 deletion public/main.css
Expand Up @@ -67,7 +67,7 @@ form label {
width: 100%;
height: 100%;
background-color: #000;
z-index1001
z-index: 1001;
-moz-opacity: 0.8;
opacity: 0.8;
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=80);
Expand Down
2 changes: 1 addition & 1 deletion public/main.styl
Expand Up @@ -71,7 +71,7 @@ form
width 100%
height 100%
background-color black
z-index1001
z-index 1001
-moz-opacity 0.8
opacity.80

Expand Down
11 changes: 11 additions & 0 deletions redisconnection.coffee
@@ -0,0 +1,11 @@

fs = require "fs"
redis = require "redis"

config = JSON.parse fs.readFileSync "./config.json"

exports.getClient = ->
client = redis.createClient(config.redis.port, config.redis.host)
if config.redis.pass?
client.auth config.redis.pass
client
1 change: 1 addition & 0 deletions resize.coffee
Expand Up @@ -29,6 +29,7 @@ exports.resize = (inputPath, outputPath, maxWidth, cb) ->
fs.writeFile outputPath, buf, (err) ->
cb?(err)

console.log "resing imgae in", inputPath
img.src = inputPath

if require.main is module
Expand Down
90 changes: 51 additions & 39 deletions server.coffee
Expand Up @@ -8,23 +8,19 @@ redis = require "redis"
form = require('connect-form')
stylus = require "stylus"
nib = require "nib"

_ = require 'underscore'
_.mixin require 'underscore.string'
kue = require "kue"
_ = require 'underscore'
_.mixin require 'underscore.string'

addCodeSharingTo = require("express-share").addCodeSharingTo

urlsortener = require "./urlsortener"
{ShareItem} = require "./shareitem"

kue = require "kue"
config = JSON.parse fs.readFileSync "./config.json"


kue.redis.createClient = ->
client = redis.createClient(config.redis.port, config.redis.host)
if config.redis.pass?
client.auth config.redis.pass
client
conn = require "./redisconnection"
kue.redis.createClient = conn.getClient

kueui = express.createServer()
kueui.use(express.basicAuth(config.kueui.user, config.kueui.pass))
Expand Down Expand Up @@ -60,24 +56,31 @@ app.shareFs __dirname + "/client/jquery.edited.coffee"
app.shareFs __dirname + "/client/main.coffee"

app.get "/#{ config.uploadpath }", (req, res) ->
res.render "upload.jade"
res.render "upload.jade",
main: {}


app.get new RegExp("^/([#{ urlsortener.alphabet }]+$)"), (req, res) ->
url = req.params[0]
jobid = urlsortener.decode url

kue.Job.get jobid, (err, job) ->
throw err if err
if not job
share = new ShareItem id: jobid, config: config
share.load (err) ->
if err
console.log err
res.end "OMG some error"
return
if not share.data.filename
res.render "404.jade", status: 404, message: "No such image"
return

noext = job.data.img.split(".")[0]
data =
imgSmalUrl: "#{ config.domain }img/#{ noext }.small.png"
imgUrl: "#{ config.domain }img/#{ job.data.img }"
res.render "image.jade", _.extend(data, job.data, config)
console.log "res", share.data
res.render "image.jade",
share: share
main:
title: share.data.caption

share.incrViews()



Expand Down Expand Up @@ -111,13 +114,21 @@ app.post "/#{ config.uploadpath }", (req, res) ->
message: "Network is missing"
return

fields.img = path.basename files.picdata.path
fields.title = "#{ fields.nick } is posting '#{ fields.caption }' to #{ fields.channel }@#{ fields.network }"
fields.filename = path.basename files.picdata.path
console.log "filename IS", fields.filename

resizeJob = jobs.create "resizeimg", fields
resizeJob.save ->
res.end JSON.stringify
url: config.domain + urlsortener.encode resizeJob.id
share = new ShareItem config: config
share.load (err) ->
throw err if err
resizeJob = jobs.create "resizeimg",
shareId: share.id
title: "#{ fields.nick } is posting '#{ fields.caption }' to #{ fields.channel }@#{ fields.network }"
share.set fields, (err) ->
throw err if err
resizeJob.save (err) ->
throw err if err
res.end JSON.stringify
url: share.getUrl()



Expand All @@ -134,19 +145,20 @@ if resizeCluster.isMaster
else
{resize} = require "./resize"
jobs.process "resizeimg", (job, done) ->
input = __dirname + "/public/img/#{ job.data.img }"
noext = job.data.img.split(".")[0]
output = __dirname + "/public/img/#{ noext }.small.png"

resize input, output, 640, (err) ->
if err
done err
else
ircJob = jobs.create "irc-#{ job.data.network.toLowerCase() }", job.data
ircJob.save (err) ->
if err
done err
else
done()
share = new ShareItem id: job.data.shareId, config: config
share.load (err) ->
return done err if err
share.set status: "resizing"
resize share.getFsPath(), share.getSmallFsPath(), 640, (err) ->
if err
done err
else
share.set status: "waiting to be posted to irc"
ircJob = jobs.create "irc-#{ share.data.network.toLowerCase() }", job.data
ircJob.save (err) ->
if err
done err
else
done()


98 changes: 98 additions & 0 deletions shareitem.coffee
@@ -0,0 +1,98 @@

redis = require "redis"
_ = require 'underscore'
_.mixin require 'underscore.string'

urlsortener = require "./urlsortener"

conn = require "./redisconnection"
client = conn.getClient()


exports.ShareItem = class ShareItem

constructor: (settings) ->
@id = settings?.id
@data =
views: 0
@config = settings?.config
if settings?.urlId
@id urlsortener.decode settings.urlId

getRedisKey: -> "share:#{ @id }"

load: (cb) ->
if not @id
client.incr "sharecount", (err, id) =>
return cb err if err
@id = id
@data.created = new Date().getTime()
@fetchData cb
else
@fetchData cb

fetchData: (cb) ->
client.hgetall @getRedisKey(), (err, data) =>
console.log "loading", data
for k, v of data
@data[k] = v
@data.views = parseInt @data.views, 10
cb err, @data

getUrlId: ->
urlsortener.encode @id

incrViews: (cb) ->
@data.views = parseInt(@data.views, 10) || 0
@data.views += 1
@save()

save: (cb) ->
client.hmset @getRedisKey(), _.clone(@data), ->
cb?.apply this, arguments

set: (ob, cb) ->
for k, v of ob
console.log "setting", k, "to", v

if typeof k is "object"
console.log "warning object key", k
continue
if typeof v is "object"
console.log "warning object value", v
continue

@data[k] = v

@save ->
console.log "SAVED!", ob
cb?.apply this, arguments

getFsPath: ->
__dirname + "/public/img/#{ @data.filename }"

getSmallFsPath: ->
noext = @data.filename.split(".")[0]
__dirname + "/public/img/#{ noext }.small.png"

getUrl: ->
@config.domain + @getUrlId()

getImgUrl: ->
"#{ @config.domain }img/#{ @data.filename }"

getSmallImgUrl: ->
noext = @data.filename.split(".")[0]
"#{ @config.domain }img/#{ noext }.small.png"



exports.ShareImg = class ShareImg


if require.main is module
fs = require "fs"
config = JSON.parse fs.readFileSync "./config.json"
s = new ShareItem id: 300, config: config
s.load ->
console.log s.data.filename
10 changes: 6 additions & 4 deletions views/image.jade
Expand Up @@ -2,8 +2,10 @@

.row
.span16.columns
h1= caption
a(href="#{ imgUrl }")
img(src="#{ imgSmalUrl }", alt="")
h1= share.data.caption
a(href="#{ share.getImgUrl() }")
img(src="#{ share.getSmallImgUrl() }", alt="")

p by #{ nick } @ #{ network }
p by #{ share.data.nick } @ #{ share.data.network }
p Page views: #{ share.data.views }
p Status: #{ share.data.status }
5 changes: 4 additions & 1 deletion views/layout.jade
@@ -1,7 +1,10 @@
!!! 5
html(lang="en")
head
title Share your images on IRC
- if (main.title)
title #{ main.title } - Share your images on IRC
- else
title Share your images on IRC
link(rel="stylesheet", type="text/css", href="/css/bootstrap.css")
link(rel="stylesheet", type="text/css", href="/main.css")
link(rel="shortcut icon", href="/favicon.png", type="image/x-icon")
Expand Down

0 comments on commit 0ddaed3

Please sign in to comment.