Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 366
- Log:
Hub integration.
- Author:
- rool
- Date:
- Sat Mar 19 22:06:05 +0000 2011
- Size:
- 4478 Bytes
1 | # Controller responsible for serving files and pictures. |
2 | |
3 | require 'zip/zip' |
4 | require 'instiki_stringsupport' |
5 | |
6 | class FileController < ApplicationController |
7 | |
8 | layout 'default' |
9 | |
10 | before_filter :check_authorized |
11 | before_filter :check_allow_uploads, :dnsbl_check, :except => [:file, :blahtex_png] |
12 | |
13 | # 2011-03-14 (ADH): Hub integration. |
14 | |
15 | @@hubssolib_permissions = HubSsoLib::Permissions.new({ |
16 | :delete => [ :admin, :webmaster ] |
17 | }) |
18 | |
19 | def FileController.hubssolib_permissions |
20 | @@hubssolib_permissions |
21 | end |
22 | |
23 | def file |
24 | @file_name = params['id'] |
25 | if params['file'] |
26 | return unless is_post and check_allow_uploads |
27 | # form supplied |
28 | new_file = @web.wiki_files.create(params['file']) |
29 | if new_file.valid? |
30 | flash[:info] = "File '#{@file_name}' successfully uploaded" |
31 | redirect_to(params['referring_page']) |
32 | else |
33 | # pass the file with errors back into the form |
34 | @file = new_file |
35 | render |
36 | end |
37 | else |
38 | # no form supplied, this is a request to download the file |
39 | file = @web.files_path.join(@file_name) |
40 | if File.exists?(file) |
41 | send_file(file) |
42 | else |
43 | return unless check_allow_uploads |
44 | @file = WikiFile.new(:file_name => @file_name) |
45 | render |
46 | end |
47 | end |
48 | end |
49 | |
50 | def blahtex_png |
51 | send_file(@web.blahtex_pngs_path.join(params['id'])) |
52 | end |
53 | |
54 | def delete |
55 | @file_name = params['id'] |
56 | file = WikiFile.find_by_file_name(@file_name) |
57 | unless file |
58 | flash[:error] = "File '#{@file_name}' not found." |
59 | redirect_to_page(@page_name) |
60 | end |
61 | system_password = params['system_password'] |
62 | if system_password |
63 | return unless is_post |
64 | # form supplied |
65 | if wiki.authenticate(system_password) |
66 | file.destroy |
67 | flash[:info] = "File '#{@file_name}' deleted." |
68 | else |
69 | flash[:error] = "System Password incorrect." |
70 | end |
71 | redirect_to_page(@page_name) |
72 | else |
73 | # no system password supplied, display the form |
74 | end |
75 | end |
76 | |
77 | def cancel_upload |
78 | return_to_last_remembered |
79 | end |
80 | |
81 | def import |
82 | if params['file'] |
83 | @problems = [] |
84 | import_file_name = "#{@web.address}-import-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}.zip" |
85 | import_from_archive(params['file'].path) |
86 | if @problems.empty? |
87 | flash[:info] = 'Import successfully finished' |
88 | else |
89 | flash[:error] = 'Import finished, but some pages were not imported:<li>' + |
90 | @problems.join('</li><li>') + '</li>' |
91 | end |
92 | return_to_last_remembered |
93 | else |
94 | # to template |
95 | end |
96 | end |
97 | |
98 | protected |
99 | |
100 | def check_authorized |
101 | if authorized? or @web.published? |
102 | return true |
103 | else |
104 | @hide_navigation = true |
105 | render(:status => 403, :text => 'This web is private', :layout => true) |
106 | return false |
107 | end |
108 | end |
109 | |
110 | def check_allow_uploads |
111 | render(:status => 404, :text => "Web #{params['web'].inspect} not found", :layout => 'error') and return false unless @web |
112 | if @web.allow_uploads? and authorized? |
113 | return true |
114 | else |
115 | @hide_navigation = true |
116 | render(:status => 403, :text => 'File uploads are blocked by the webmaster', :layout => true) |
117 | return false |
118 | end |
119 | end |
120 | |
121 | private |
122 | |
123 | def import_from_archive(archive) |
124 | logger.info "Importing pages from #{archive}" |
125 | zip = Zip::ZipInputStream.open(archive) |
126 | while (entry = zip.get_next_entry) do |
127 | ext_length = File.extname(entry.name).length |
128 | page_name = entry.name[0..-(ext_length + 1)].purify |
129 | page_content = entry.get_input_stream.read.purify |
130 | logger.info "Processing page '#{page_name}'" |
131 | begin |
132 | existing_page = @wiki.read_page(@web.address, page_name) |
133 | if existing_page |
134 | if existing_page.content == page_content |
135 | logger.info "Page '#{page_name}' with the same content already exists. Skipping." |
136 | next |
137 | else |
138 | logger.info "Page '#{page_name}' already exists. Adding a new revision to it." |
139 | wiki.revise_page(@web.address, page_name, page_name, page_content, Time.now, @author, PageRenderer.new) |
140 | end |
141 | else |
142 | wiki.write_page(@web.address, page_name, page_content, Time.now, @author, PageRenderer.new) |
143 | end |
144 | rescue => e |
145 | logger.error(e) |
146 | @problems << "#{page_name} : #{e.message}" |
147 | end |
148 | end |
149 | logger.info "Import from #{archive} finished" |
150 | end |
151 | |
152 | end |