Document #1

Posted by gnrfan on Wed Dec 12 2007 23:20:02 GMT-0800 (PST) from 190.40.126.76.
permalink | raw | download | new post

1. /* appjet:version 0.1 */
2.
3. /*** Very basic pastebin by Antonio Ognio <gnrfan@gmail.com> ***/
4.
5. import("storage");
6.
7. /* Helper functions */
8.
9. function get_ipaddr() {
10.    return request.clientAddr;
11. }
12.
13. function get_timestamp() {
14.    return Date().toString();
15. }
16.
17. function get_hostname() {
18.    return appjet.appName + "." + appjet.mainDomain;
19. }
20.
21. function format_url(path) {
22.    hostname = get_hostname();
23.    return "http://" + hostname + path;
24. }
25.
26. function str_trim(str) {
27.    return str.replace(/^\s+|\s+$/g, '');  
28. }
29.
30. /* Storage functions */
31.
32. function get_counter() {
33.  
34.    if (! storage.counter) {
35.            storage.counter = 1;
36.    }
37.    value = storage.counter;
38.    storage.counter = storage.counter + 1;
39.    return value;
40.  
41. }
42.
43. function create_document(params) {
44.    var document = new StorableObject();
45.    params.author = str_trim(params.author)
46.    if (params.author.length == 0) {
47.        params.author = "Anonymous";
48.    }
49.    document.docid = get_counter();
50.    document.body = params.body;
51.    document.author = params.author;
52.    document.ipaddr = get_ipaddr();
53.    document.created = get_timestamp();
54.    documents = get_documents();
55.    documents.add(document);
56.    return document.docid;
57. }
58.
59. function get_documents() {
60.    if (! storage.documents) {
61.        storage.documents = new StorableCollection();  
62.    }
63.    return storage.documents;
64. }
65.
66. function get_document_count() {
67.    documents =get_documents();
68.    return documents.size();
69. }
70.
71. function document_exists(docid) {
72.    documents = get_documents();
73.    if (documents.filter({docid: docid}).size()>0) {
74.        return true;
75.    } else {
76.        return false;
77.    }
78. }
79.
80. function get_document(docid) {
81.    result = false;
82.    documents = get_documents();
83.    view = documents.filter({docid: docid});
84.    view.forEach(function (doc) {
85.        if (doc.docid == docid) {
86.            result = doc;
87.        }
88.    });
89.    return result;
90. }
91.
92. /* URL functions */
93.
94. function get_url() {
95.    return request.path;
96. }
97.
98. function url_is_frontpage() {
99.    url = get_url();
100.    if (url == "/") {
101.        return true;
102.    } else {
103.        return false;
104.    }
105. }
106.
107. function url_is_document() {
108.    url = get_url();
109.    return url.match(/^\/[1-9][0-9]*$/g);
110. }
111.
112. function url_is_raw() {
113.    url = get_url();
114.    return url.match(/^\/raw\/[1-9][0-9]*$/g);
115. }
116.
117. function url_is_download() {
118.    url = get_url();
119.    return url.match(/^\/download\/[1-9][0-9]*$/g);
120. }
121.
122.
123. function url_is_favicon() {
124.    url = get_url();
125.    return url.match(/^\/favicon.ico$/g);
126. }
127.
128. function url_not_found() {
129.    if (url_is_frontpage()) {
130.        return false;
131.    }
132.    if (url_is_document()) {
133.        return false;
134.    }
135.    if (url_is_raw()) {
136.        return false;
137.    }
138.    if (url_is_download()) {
139.        return false;
140.    }
141.    if (url_is_favicon()) {
142.        return false;
143.    }
144.    return true;
145. }
146.
147. function get_document_from_url() {
148.    if (url_is_document()) {
149.        url = get_url();
150.        matches = url.match(/^\/([1-9][0-9]*)$/);
151.        return matches[1];
152.    } else {
153.        return false;
154.    }
155. }
156.
157. function get_document_from_raw_url() {
158.    if (url_is_raw()) {
159.        url = get_url();
160.        matches = url.match(/^\/raw\/([1-9][0-9]*)$/);
161.        return matches[1];
162.    } else {
163.        return false;
164.    }
165. }
166.
167. function get_document_from_download_url() {
168.    if (url_is_download()) {
169.        url = get_url();
170.        matches = url.match(/^\/download\/([1-9][0-9]*)$/);
171.        return matches[1];
172.    } else {
173.        return false;
174.    }
175. }
176.
177. function get_new_post_url() {
178.    path = "/";
179.    return format_url(path);
180. }
181.
182. function get_document_url(docid) {
183.    path = "/" + docid;
184.    return format_url(path);
185. }
186.
187. function get_raw_document_url(docid) {
188.    path = "/raw/" + docid;
189.    return format_url(path);
190. }
191.
192. function get_download_document_url(docid) {
193.    path = "/download/" + docid;
194.    return format_url(path);
195. }
196.
197. /* Document Rendering functions */
198.
199. function add_favicon() {
200.    page.head.write("<link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/favicon.ico\" />");
201. }
202.
203. function render_as_raw(docid) {
204.    doc = get_document(docid);
205.    if (doc == false) {
206.        return false;
207.    }
208.    page.setMode("plain");    
209.      response.setHeader("Content-type", "text/plain");
210.      print(raw(doc.body));
211. }
212.
213. function render_as_download(docid) {
214.    doc = get_document(docid);
215.    if (doc == false) {
216.        return false;
217.    }
218.    page.setMode("plain");
219.    prefix = "pastebin.appjet.com-";
220.    c_d_value = sprintf("attachment; filename=\"%s%s.txt\"", prefix, docid);
221.      response.setHeader("Content-Disposition", c_d_value);
222.      print(raw(doc.body));
223. }
224.
225. function render_document(docid) {
226.  
227.    add_favicon();
228.  
229.    doc = get_document(docid);
230.    if (doc == false) {
231.        return false;
232.    }
233.
234.    url = get_document_url(docid);  
235.    raw_url = get_raw_document_url(docid);
236.    download_url = get_download_document_url(docid);
237.    new_url = get_new_post_url();  
238.  
239.    print(H1("Document #", docid));
240.                  
241.    print("Posted by ",B(I(doc.author))," on ", B(I(doc.created), " from ", doc.ipaddr, "."),BR());
242.  
243.    separator = " | ";
244.    print(link(url,"permalink"), separator, link(raw_url,"raw"), separator, link(download_url, "download"), separator,  link(new_url, "new post"), BR());
245.  
246.    print(BR());
247.  
248.    lines = doc.body.split("\n");
249.  
250.    print(raw("<table>\n"));
251.  
252.    color1 = "#ffffff";
253.    color2 = "#eeeeee";
254.  
255.    color = color1;
256.    for (line in lines) {
257.         line_number = parseInt(line) + 1;
258.        print(TR(TD({align:"right",bgcolor: color2},I(line_number),". "), TD({bgcolor:color},lines[line])));
259.        if (color == color1) {
260.            color = color2;
261.        } else {
262.            color = color1;
263.        }  
264.    }
265.  
266.    print(raw("</table>\n"));
267. }
268.
269. function render_frontpage() {
270.    add_favicon();
271.  
272.    new_post_url = get_new_post_url();
273.  
274.    print(H1("Pastebin AppJet App"));
275.  
276.    document_count = get_document_count() ;
277.  
278.    if (document_count>0) {
279.        if (document_count>1) {
280.             print(P(I(document_count, " documents proudly stored on our repository.")));
281.        } else {
282.            print(P(I(document_count, " document proudly stored on our repository.")));
283.        }
284.    } else {
285.        print(P(I("No documents have been posted. Be the first to post!")));
286.    }
287.  
288.    print(
289.        FORM({method: 'post', action: new_post_url},
290.            P("Paste the text of the document here:"),
291.            TEXTAREA(
292.                {name: 'body', rows: 20, cols:70, style: "width: 90%;"}
293.            ),
294.            BR(),
295.            B("Your name: "),
296.            INPUT({type: 'text', name: 'author', value: 'Anonymous'}),
297.            " ",
298.            INPUT({type: 'submit', value: 'Submit'})
299.        )
300.    );
301. }
302.
303. function render_favicon() {
304.    url = "http://pastebin.com/favicon.ico";
305.    response.redirect(url)
306. }
307.
308. function render_not_found() {
309.    url = get_url();  
310.    response.notFound();
311. }
312.
313. /* Main routine */
314.
315. if (url_is_frontpage()) {
316.  
317.    /* Handle document creation */
318.  
319.    if (request.params.body) {
320.      
321.        params = {
322.            body: request.params.body,
323.            author: request.params.author
324.        }
325.      
326.        docid = create_document(params);
327.        document_url = get_document_url(docid);
328.        response.redirect(document_url);
329.      
330.    }
331.  
332.    render_frontpage();
333. }
334.
335. if (url_is_document()) {
336.    docid = get_document_from_url();
337.    if (document_exists(docid)) {
338.        render_document(docid);
339.    } else {
340.        render_not_found();
341.    }
342. }
343.
344. if (url_is_raw()) {
345.    docid = get_document_from_raw_url();  
346.     if (document_exists(docid)) {
347.        render_as_raw(docid);
348.    } else {
349.        render_not_found();
350.    }
351. }
352.
353. if (url_is_download()) {
354.    docid = get_document_from_download_url();  
355.     if (document_exists(docid)) {
356.        render_as_download(docid);
357.    } else {
358.        render_not_found();
359.    }
360. }
361.
362.
363. if (url_is_favicon()) {
364.    render_favicon();
365. }
366.
367. if (url_not_found()) {
368.    render_not_found();
369. }
370.
Powered by AppJet
source
rendered in 0.218s