Skip to content

Instantly share code, notes, and snippets.

@terotil
Last active August 29, 2015 14:06
Show Gist options
  • Save terotil/b4688f7529df21ac82e1 to your computer and use it in GitHub Desktop.
Save terotil/b4688f7529df21ac82e1 to your computer and use it in GitHub Desktop.
Sup publish hook to save attachment to download location
# Copyright (c) 2014 Tero Tilus
# Published under MIT license http://opensource.org/licenses/MIT
# It is strongly recommended to have http://my.host/pub/root/ behind
# authentication and have a cronjob automatically cleaning up old files
# from /local/path/to/pub/root/
$pub_url_base ||= 'http://my.host/pub/root/'
$pub_save_dir ||= '/local/path/to/pub/root/'
def sanitize_filename(filename)
filename.gsub(/[^a-zA-Z0-9åäöÅÄÖ_.-]/, '_').gsub(/_+/, '_')
end
def save_to_file fn
if File.exists? fn
unless BufferManager.ask_yes_or_no "File \"#{fn}\" exists. Overwrite?"
info "Not overwriting #{fn}"
return
end
end
begin
File.open(fn, "w") { |f| yield f }
true
rescue SystemCallError, IOError => e
m = "Error writing file: #{e.message}"
info m
BufferManager.flash m
false
end
end
def publish_www(chunk)
content = ''
default_fn = nil
case chunk
when Redwood::Message
default_fn = $pub_save_dir + sanitize_filename(chunk.subj[0..30]) + '.msg'
chunk.each_raw_message_line { |l| content += l }
when Redwood::Chunk::Attachment
default_fn = $pub_save_dir + chunk.filename
content = chunk.raw_content
else
BufferManager.flash 'Dunno how to publish '+chunk.class.name
return false
end
fn = BufferManager.ask_for_filename :filename, "Published name: ", default_fn
if save_to_file(fn) { |f| f.print content }
pub_url = fn.sub($pub_save_dir, $pub_url_base)
say "Saved #{fn}, accessible at #{pub_url}"
BufferManager.flash pub_url
return true
end
return false
end
publish_www(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment