Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fix various issues

  • Loading branch information...
commit cb3ad257b743b7423b4edd4c947ad4bd2f6bc48f 1 parent b65540d
Alok Menghrajani authored
6 amazon_s3.opa
@@ -82,7 +82,7 @@ module AmazonS3 {
82 82
83 83 function put(string bucket, string path, string mimetype, string data) {
84 84 headers = computeSignature("PUT", "", mimetype, bucket, path)
85   - WebClient.Put.try_put_with_options(
  85 + _ = WebClient.Put.try_put_with_options(
86 86 Option.get(Uri.of_string("http://{bucket}.s3.amazonaws.com/{path}")),
87 87 data,
88 88 {
@@ -96,12 +96,14 @@ module AmazonS3 {
96 96 ssl_policy: {none},
97 97 }
98 98 )
  99 +
99 100 // todo: convert response to AmazonS3.response
100 101 {success}
101 102 }
102 103
103 104 function string computeSignature(string method, string md5, string mimetype, string bucket, string path) {
104   - date_printer = Date.generate_printer("%a, %0d %b %Y %T PST")
  105 + timezone = AmazonS3Auth.timezone
  106 + date_printer = Date.generate_printer("%a, %0d %b %Y %T {timezone}")
105 107 string date = Date.to_formatted_string(date_printer, Date.now())
106 108 string sts = "{method}\n{md5}\n{mimetype}\n{date}\n/{bucket}/{path}"
107 109 string hmac = Crypto.Base64.encode(Crypto.Hash.hmac_sha1(AmazonS3Auth.private_key, sts))
5 amazon_s3_auth.opa.sample
... ... @@ -1,4 +1,5 @@
1 1 module AmazonS3Auth {
2   - public_key = "44CF9590006BF252F707"
3   - private_key = "OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV"
  2 + server string public_key = "44CF9590006BF252F707"
  3 + server string private_key = "OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV"
  4 + server string timezone = "PST"
4 5 }
9 amazon_s3_test.opa
... ... @@ -1,3 +1,6 @@
1   -AmazonS3.put("pixlpaste", "test", "text/plain", "hello world")
2   -Debug.warning(AmazonS3.get("pixlpaste", "test"))
3   -AmazonS3.delete("pixlpaste", "test")
  1 +//AmazonS3.put("pixlpaste", "test", "text/plain", "hello world")
  2 +//Debug.warning(AmazonS3.get("pixlpaste", "test"))
  3 +//AmazonS3.delete("pixlpaste", "test")
  4 + date_printer = Date.generate_printer("%a, %0d %b %Y %T %z")
  5 + string date = Date.to_formatted_string(date_printer, Date.now())
  6 +Debug.dump(date)
106 pixlpaste.opa
@@ -12,8 +12,6 @@
12 12 * - base64 decode the data on the client side before uploading.
13 13 * => waiting for: opa bug fix
14 14 *
15   - * - convert to jpg
16   - *
17 15 * - Fix date (hard coded UTC) issue
18 16 *
19 17 * - about page, help page
@@ -27,20 +25,22 @@
27 25 *
28 26 * - clean up css. Improve centering code?
29 27 *
30   - * - handle content-type
  28 + * - improve urls we generate
31 29 *
32 30 * To compile:
33 31 * - debug:
34   - * opa-plugin-builder -o pixlpaste_binding pixlpaste_binding.js; opa --parser js-like pixlpaste_binding.opp pixlpaste.opa --
  32 + * opa-plugin-builder -o pixlpaste_binding pixlpaste_binding.js
  33 + * opa --parser js-like pixlpaste_binding.opp amazon_s3.opa amazon_s3_auth.opa pixlpaste.opa --
35 34 *
36 35 * - release:
37 36 * opa-plugin-builder -o pixlpaste_binding pixlpaste_binding.js
38   - * opa --parser js-like --compile-release pixlpaste_binding.opp pixlpaste.opa
  37 + * opa --parser js-like --compile-release pixlpaste_binding.opp amazon_s3.opa amazon_s3_auth.opa pixlpaste.opa
39 38 * sudo nohup ./pixlpaste.exe -p 80 & disown
40 39 */
41 40
42 41 import stdlib.web.client
43 42 import stdlib.crypto
  43 +import stdlib.web.canvas
44 44
45 45 type pixel = {
46 46 intmap(string) data,
@@ -55,13 +55,6 @@ type upload_info = {
55 55
56 56 database stringmap(pixel) /pixels
57 57
58   -type s3_credentials = {
59   - string private_key,
60   - string public_key
61   -}
62   -
63   -database s3_credentials /s3
64   -
65 58 client hook_paste = %%pixlpaste_binding.hook_paste%%
66 59 client hook_drop = %%pixlpaste_binding.hook_drop%%
67 60 client hook_file_chooser = %%pixlpaste_binding.hook_file_chooser%%
@@ -123,53 +116,31 @@ function void render_preview(string data) {
123 116 })
124 117 }
125 118
126   -/**
127   - * EC2 stuff
128   - */
129   -exposed server function resource s3_save_credentials(s3_credentials new_credentials) {
130   - if (Db.exists(@/s3)) {
131   - Resource.raw_text("already saved: {(/s3).public_key}")
132   - } else {
133   - /s3 <- new_credentials
134   - Resource.raw_text("credentials saved!")
135   - }
136   -}
137   -
138 119 exposed server function s3_upload_data(string id) {
139 120 p = /pixels[id];
  121 +
140 122 string data = Map.fold(
141 123 function(_, v, r) {
142 124 String.concat("", [r, v])
143 125 },
144 126 p.data,
145   - "");
146   -
147   - string mimetype = "image/png"
148   - date_printer = Date.generate_printer("%a, %0d %b %Y %T PST")
149   - string date = Date.to_formatted_string(date_printer, Date.now())
150   - string sts = "PUT\n\n{mimetype}\n{date}\n/pixlpaste/pixels/{id}"
151   - string public_key = (/s3).public_key
152   - string private_key = (/s3).private_key
153   -
154   - string hmac = Crypto.Base64.encode(Crypto.Hash.hmac_sha1(private_key, sts));
155   -
156   - string headers = "Date: {date}\nAuthorization: AWS {public_key}:{hmac}";
157   -
158   - result = WebClient.Put.try_put_with_options(
159   - Option.get(Uri.of_string("http://pixlpaste.s3.amazonaws.com/pixels/{id}")),
160   - data,
161   - {
162   - auth: {none},
163   - custom_headers: {some:headers},
164   - mimetype: mimetype,
165   - custom_agent: {none},
166   - redirect_to_get: {none},
167   - timeout_sec: {none},
168   - ssl_key: {none},
169   - ssl_policy: {none}
170   - }
171   - )
172   - // Todo: handle errors!
  127 + ""
  128 + );
  129 +
  130 + // data is in the following format:
  131 + // data:image/<png|jpeg|etc.>;base64,<base64 encoded data>
  132 + int offset = Option.get(String.index(";base64,", data))+8
  133 + mimetype = String.sub(5, offset-5-8, data)
  134 + data = String.sub(offset, String.length(data)-offset, data)
  135 + data = Crypto.Base64.decode(data)
  136 +
  137 + AmazonS3.put("pixlpaste", "pixels/{id}", mimetype, data)
  138 + // TODO: handle errors
  139 +
  140 + // Delete local data, since S3 now has the data
  141 + // TODO: implement file not found logic against S3
  142 + Db.remove(@/pixels[id]/data)
  143 +
173 144 void
174 145 }
175 146
@@ -178,17 +149,6 @@ client function void upload_data() {
178 149
179 150 string data = Option.get(Dom.get_property(#preview, "src"));
180 151
181   - // data is in the following format:
182   - // data:image/<png|jpeg|etc.>;base64,<base64 encoded data>
183   - // for now, we'll only locate ";base64," and ignore the first part
184   - // we'll tell the browser the image is image/png, even if that's
185   - // not the case (browsers are smart enough to figure things out)
186   -
187   - // base64 decode the data on the client side. This will speed up the upload
188   - int offset = Option.get(String.index(";base64,", data)) + 8
189   - data = String.sub(offset, String.length(data)-offset, data)
190   - data = Crypto.Base64.decode(data)
191   -
192 152 // For now we must upload the data in base64, due to a bug in the framework
193 153 int length = String.length(data);
194 154
@@ -219,14 +179,14 @@ client function void upload_data() {
219 179 upload_data_aux(data, length, piece_length, next_info);
220 180 } else {
221 181 // We are done :)
222   - s3_upload_data(info.id);
  182 + s3_upload_data(info.id)
223 183 Client.goto("/{info.id}");
224 184 }
225 185 }
226 186
227 187 exposed function upload_info upload_first_piece(upload_info info, string piece) {
228 188 // TODO: what if id is already taken?
229   - string id = Random.generic_string("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 4);
  189 + string id = Random.generic_string("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 6);
230 190 string secret = Random.string(10);
231 191 intmap data = Map.empty;
232 192 data = Map.add(0, piece, data);
@@ -437,6 +397,7 @@ function resource display_pixlpaste() {
437 397 <div id=#label class="help-block hidden"/>
438 398 </div></div></div>
439 399 </div></div></div>
  400 + <canvas id="canvas" style="border: 1px solid black"/>
440 401 </>
441 402 );
442 403 }
@@ -454,21 +415,6 @@ function resource start(Uri.relative uri) {
454 415 Resource.raw_text("google-site-verification: googlee4b78291cdd3f153.html")
455 416 case {path:{hd:"robots.txt" ...} ...}:
456 417 Resource.raw_text("User-agent: *\nAllow: /\n")
457   - case {path:{hd:"s3" ...}, query:query ...}:
458   - cred = List.fold(
459   - function s3_credentials ((string k, string v), s3_credentials c) {
460   - if ((c.public_key == "") && (k == "public_key")) {
461   - {private_key:c.private_key, public_key:v};
462   - } else if ((c.private_key == "") && (k == "private_key")) {
463   - {private_key:v, public_key:c.public_key};
464   - } else {
465   - c;
466   - }
467   - },
468   - query,
469   - {private_key:"", public_key:""}
470   - )
471   - s3_save_credentials(cred)
472 418 case {path:{hd:"local", ~tl} ...}:
473 419 match (tl) {
474 420 case {~hd ...}:

0 comments on commit cb3ad25

Please sign in to comment.
Something went wrong with that request. Please try again.