js/play.js
author deusx
Sun Apr 23 03:52:09 2006 +0000 (4 years ago)
branchS3Ajax
changeset 11 7006d3de0ce0
parent 58af45ef3b03c
permissions -rw-r--r--
[svn r760] [wiki:S3Ajax]: Added an anonymous option to the wiki
     1 /**
     2     Play around with S3Ajax!
     3 
     4     TODO: Remove Dojo dependency
     5 */
     6 
     7 if (window.dojo) {
     8     // dojo.setModulePrefix("dojo", "js/dojo");
     9     // dojo.require("dojo.io.*");
    10     // dojo.require("dojo.crypto.SHA1");
    11     dojo.require("dojo.storage.*");
    12 }
    13 
    14 Play = {
    15 
    16     /**
    17         Initialize the play app.
    18     */
    19     init: function() {
    20         var _this = this;
    21 
    22         S3Ajax.DEBUG = true;
    23 
    24         // HACK: Wait for the storage flash to become available.
    25         // TODO: Look for an official Dojo event to make this happen.
    26         if (window.dojo) {
    27             this._storage_wait = setInterval(function() {
    28                 if ($('dojo-storeContainer')) {
    29                     clearInterval(_this._storage_wait);
    30                     _this._storage_wait = null;
    31                     _this.recall();
    32                 }
    33             }, 100);
    34         }
    35     },
    36     
    37     /**
    38         Make an attempt to recall S3 credentials.
    39     */
    40     recall: function() {
    41         if (window.dojo && dojo.storage.get('key_id', '/S3Ajax')) {
    42             S3Ajax.KEY_ID     = dojo.storage.get('key_id',     '/S3Ajax');
    43             S3Ajax.SECRET_KEY = dojo.storage.get('secret_key', '/S3Ajax');
    44             $('key_id').value = S3Ajax.KEY_ID;
    45             $('update_msg').innerHTML = 'Credentials fetched from local storage: '+(new Date());
    46 
    47             // this.listbuckets();
    48             // this.list();
    49         }
    50     },
    51 
    52     /**
    53         Accept new credentials from the form in the page.
    54     */
    55     login: function() {
    56         S3Ajax.KEY_ID     = $('key_id').value;
    57         S3Ajax.SECRET_KEY = $('secret_key').value;
    58 
    59         // Try to stash the credentials away for next time.
    60         if (window.dojo) {
    61             dojo.storage.set('key_id',     S3Ajax.KEY_ID,     '/S3Ajax');
    62             dojo.storage.set('secret_key', S3Ajax.SECRET_KEY, '/S3Ajax');
    63         }
    64 
    65         $('update_msg').innerHTML = 'Last updated: '+(new Date());
    66     },
    67 
    68     /**
    69         Download the specified resource.
    70     */
    71     download: function() {
    72         var bucket = $('list_bucket').value;
    73         var key    = $('key').value;
    74 
    75         $('content').value = "Loading...";
    76 
    77         S3Ajax.get(bucket, key,
    78             function(req, content) {
    79                 $('content').value = content;
    80                 $('xfer_msg').innerHTML = "Download succeeded at "+(new Date());
    81             },
    82             function(req, objc) {
    83                 $('xfer_msg').innerHTML = "Download failed at "+(new Date());
    84             }
    85         );
    86     },
    87 
    88     /**
    89         Upload the specified resource.
    90     */
    91     upload: function() {
    92         var bucket   = $('list_bucket').value;
    93         var key      = $('key').value;
    94         var content  = $('content').value;
    95 
    96         S3Ajax.put(bucket, key, content,
    97             function(req) {
    98                 $('xfer_msg').innerHTML = "Upload succeeded at "+(new Date());
    99             },
   100             function(req, obj) {
   101                 $('xfer_msg').innerHTML = "Upload failed at "+(new Date());
   102             }
   103         );
   104 
   105         /*
   106         S3Ajax.put(bucket, key, content,
   107             {
   108                 content_type: "text/plain",
   109                 meta:         {'posted-by':'S3Ajax'},
   110                 acl:          "public-read",
   111             },
   112             function(req) {
   113                 $('xfer_msg').innerHTML = "Upload succeeded at "+(new Date());
   114             },
   115             function(req, obj) {
   116                 $('xfer_msg').innerHTML = "Upload failed at "+(new Date());
   117             }
   118         );
   119         */
   120     },
   121 
   122     /**
   123         List available buckets
   124     */
   125     listbuckets: function() {
   126         var _this = this;
   127 
   128         setList('buckets_list',['Loading...','']);
   129 
   130         S3Ajax.listBuckets(
   131             function(req, obj) {
   132                 clearList('buckets_list');
   133                 if (obj.ListAllMyBucketsResult) {
   134                     var buckets = obj.ListAllMyBucketsResult.Buckets.Bucket;
   135                     for (var i=0, bucket; bucket=buckets[i]; i++) {
   136                         addToList(
   137                             'buckets_list',
   138                             bucket.Name + ' ['+bucket.CreationDate+']',
   139                             bucket.Name
   140                         );
   141                     }
   142                 }
   143                 keys_list = null;
   144             },
   145             function(req) {
   146                 setList('buckets_list', ["Buckets list failed at "+(new Date()),'']);
   147             }
   148         );
   149 
   150     },
   151 
   152     /**
   153         Change the current bucket to one selected in list.
   154     */
   155     selectbucket: function() {
   156         var sel = getSelected('buckets_list');
   157         if (sel.length) $('list_bucket').value = sel[0]; 
   158     },
   159 
   160     /**
   161     */
   162     deletebucket: function() {
   163         var _this = this;
   164         var sel = getSelected('buckets_list');
   165         if (!sel.length) return;
   166 
   167         setList('buckets_list',['Deleting...','']);
   168         S3Ajax.deleteBucket(sel[0], function() {
   169             _this.listbuckets();
   170         });
   171     },
   172 
   173     /**
   174     */
   175     createbucket: function() {
   176         var _this = this;
   177         var bucket = $('list_bucket').value;
   178 
   179         setList('buckets_list',['Creating...','']);
   180         S3Ajax.createBucket(bucket, function() {
   181             _this.listbuckets();
   182         });
   183     },
   184 
   185     /**
   186         List a bucket's contents.
   187     */
   188     list: function() {
   189         var _this = this;
   190         setList('keys_list', ['Loading...','']);
   191 
   192         var bucket = $('list_bucket').value;
   193 
   194         var params = {};
   195         if ($('list_prefix').value)  params['prefix']   = $('list_prefix').value;
   196         if ($('list_maxkeys').value) params['max-keys'] = $('list_maxkeys').value;
   197         if ($('list_marker').value)  params['marker']   = $('list_marker').value;
   198 
   199         S3Ajax.listKeys(bucket, params, 
   200             function(req, obj) {
   201                 clearList('keys_list');
   202                 var contents = obj.ListBucketResult.Contents;
   203                 for (var i=0, item; item=contents[i]; i++) {
   204                     addToList(
   205                         'keys_list',
   206                         /*item.LastModified + ' ' +*/ item.Key + ' (' + item.Size + ')',
   207                         item.Key
   208                     );
   209                 }
   210             },
   211             function(req, obj) {
   212                 setList('keys_list', ["Keys list failed at "+(new Date()),'']);
   213             }
   214         );
   215     },
   216 
   217     /**
   218         Download the selected key.
   219     */
   220     downloadSelectedKey: function() {
   221         var sel = getSelected('keys_list');
   222         if (sel.length) {
   223             $('key').value = sel[0]; 
   224             this.download();
   225         }
   226     },
   227 
   228     /**
   229         Delete seleted keys.
   230     */
   231     deleteSelectedKeys: function() {
   232         var _this = this;
   233         var sel_keys = getSelected('keys_list');
   234 
   235         if (!window.confirm("Delete " + sel_keys.length + " selected items?")) return;
   236 
   237         S3Ajax.deleteKeys($('list_bucket').value, sel_keys, 
   238             function(key)      { /*logFire("Deleted "+key);*/ },
   239             function(req, obj) { /*logFire("Deleted all");*/ _this.list(); }
   240         );
   241     },
   242 
   243     /* Help protect against errant end-commas */
   244     EOF: null
   245 }
   246 addLoadEvent(function(){ Play.init() });