From 5700d4513ff6a22eeb8fa5744a9447086126606b Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Tue, 30 Dec 2008 03:03:02 -0600 Subject: [PATCH] Preliminary (?) Interface for Deleting Uploaded Files. The simplest thing which could possibly work ... --- app/controllers/file_controller.rb | 38 ++++++++++++++++++++++++++---- app/views/file/delete.html.erb | 18 ++++++++++++++ config/routes.rb | 1 + lib/chunks/wiki.rb | 2 +- lib/url_generator.rb | 12 ++++++++++ public/stylesheets/instiki.css | 11 +++++++++ 6 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 app/views/file/delete.html.erb diff --git a/app/controllers/file_controller.rb b/app/controllers/file_controller.rb index 1e25bf62..ce5f560f 100644 --- a/app/controllers/file_controller.rb +++ b/app/controllers/file_controller.rb @@ -12,11 +12,7 @@ class FileController < ApplicationController def file @file_name = params['id'] if params['file'] - unless (request.post? || ENV["RAILS_ENV"] == "test") - headers['Allow'] = 'POST' - render(:status => 405, :text => 'You must use an HTTP POST', :layout => 'error') - return - end + return unless is_post # form supplied new_file = @web.wiki_files.create(params['file']) if new_file.valid? @@ -38,6 +34,29 @@ class FileController < ApplicationController end end end + + def delete + @file_name = params['id'] + file = WikiFile.find_by_file_name(@file_name) + unless file + flash[:error] = "File '#{@file_name}' not found." + redirect_to_page(@page_name) + end + system_password = params['system_password'] + if system_password + return unless is_post + # form supplied + if wiki.authenticate(system_password) + file.destroy + flash[:info] = "File '#{@file_name}' deleted." + else + flash[:error] = "System Password incorrect." + end + redirect_to_page(@page_name) + else + # no system password supplied, display the form + end + end def cancel_upload return_to_last_remembered @@ -75,6 +94,15 @@ class FileController < ApplicationController private + def is_post + unless (request.post? || ENV["RAILS_ENV"] == "test") + headers['Allow'] = 'POST' + render(:status => 405, :text => 'You must use an HTTP POST', :layout => 'error') + return false + end + return true + end + def import_from_archive(archive) logger.info "Importing pages from #{archive}" zip = Zip::ZipInputStream.open(archive) diff --git a/app/views/file/delete.html.erb b/app/views/file/delete.html.erb new file mode 100644 index 00000000..600352c4 --- /dev/null +++ b/app/views/file/delete.html.erb @@ -0,0 +1,18 @@ +<%- + @title = "Delete #{@file_name}" + @hide_navigation = true +-%> + +<%= error_messages_for 'delete' %> + +<% form_tag({ :controller => 'file', :web => @web_name, :action => 'delete' }, + {'accept-charset' => 'utf-8' }) do %> +
+ <%= hidden_field 'file', 'file_name' %> + + +
+ or go <%= link_to "back", :back %> to the page you came from. +
+<%- end -%> diff --git a/config/routes.rb b/config/routes.rb index 7a245e95..11dfee9a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,6 +17,7 @@ ActionController::Routing::Routes.draw do |map| connect_to_web map, ':web/edit_web', :controller => 'admin', :action => 'edit_web' connect_to_web map, ':web/remove_orphaned_pages', :controller => 'admin', :action => 'remove_orphaned_pages' connect_to_web map, ':web/remove_orphaned_pages_in_category', :controller => 'admin', :action => 'remove_orphaned_pages_in_category' + connect_to_web map, ':web/file/delete/:id', :controller => 'file', :action => 'delete', :requirements => {:id => /[-._\w]+/}, :id => nil connect_to_web map, ':web/files/:id', :controller => 'file', :action => 'file', :requirements => {:id => /[-._\w]+/}, :id => nil connect_to_web map, ':web/import/:id', :controller => 'file', :action => 'import' connect_to_web map, ':web/login', :controller => 'wiki', :action => 'login' diff --git a/lib/chunks/wiki.rb b/lib/chunks/wiki.rb index 7df6f3f0..93e9f34d 100644 --- a/lib/chunks/wiki.rb +++ b/lib/chunks/wiki.rb @@ -105,7 +105,7 @@ module WikiChunk unless defined? WIKI_LINK WIKI_LINK = /(":)?\[\[\s*([^\]\s][^\]]*?)\s*\]\]/ - LINK_TYPE_SEPARATION = Regexp.new('^(.+):((file)|(pic))$', 0) + LINK_TYPE_SEPARATION = Regexp.new('^(.+):((file)|(pic)|(delete))$', 0) ALIAS_SEPARATION = Regexp.new('^(.+)\|(.+)$', 0) WEB_SEPARATION = Regexp.new('^(.+):(.+)$', 0) end diff --git a/lib/url_generator.rb b/lib/url_generator.rb index 44bac253..e8457bee 100644 --- a/lib/url_generator.rb +++ b/lib/url_generator.rb @@ -33,6 +33,8 @@ class AbstractUrlGenerator file_link(mode, name, text, web.address, known_page, description) when :pic pic_link(mode, name, text, web.address, known_page) + when :delete + delete_link(mode, name, web.address, known_page) else raise "Unknown link type: #{link_type}" end @@ -129,4 +131,14 @@ class UrlGenerator < AbstractUrlGenerator end end + def delete_link(mode, name, web_address, known_file) + href = @controller.url_for :controller => 'file', :web => web_address, + :action => 'delete', :id => name + if mode == :show and known_file + %{Delete #{name}} + else + %{[[#{name}:delete]]} + end + end + end diff --git a/public/stylesheets/instiki.css b/public/stylesheets/instiki.css index a3508610..58f88e10 100644 --- a/public/stylesheets/instiki.css +++ b/public/stylesheets/instiki.css @@ -88,6 +88,17 @@ background-color:#DDD; background-color:#FFF; } +.deleteWikiWord { +background-color:#FDD; +border: 2px solid red; +padding: 2px; +} + +.deleteWikiWord:hover, .deleteWikiWord a:hover { +background-color:#FAA; +} + + form#navigationSearchForm { display:inline; }