nextwebgen.com

The Next Generation Web Now

YouTube Uploader now using Gears, and what people missed in Gears 0.4

Filed under: Uncategorized — Dion Almaer at 9:20 am on Wednesday, August 27, 2008

While we posted about the Gears 0.4 features a lot of the press only really talked about the Geolocation piece.

I think that is important, and posted on that too, but Brad’s piece discussed the full gamut including the Blob API, resummable HTTP, and Desktop API improvements to allow controlled file system access. The example that Brad built was the upload movie tool which ties this all together.

Check out the source code and you will see the parts and pieces in action:

Geolocation

JAVASCRIPT:

  1.  
  2. MovieTool.prototype.getLocation = function(callback) {
  3.   var geolocation = google.gears.factory.create(‘beta.geolocation’);
  4.   if (!geolocation.getPermission(‘Upload Movie Tool’, ‘’,
  5.                                   ‘This sample can use your ‘
  6.                                 + ‘geographic coordinates in order to tag ‘
  7.                                 + ‘uploaded videos with your location’)) {
  8.     document.getElementById(‘location’).innerHTML = ‘Permission denied’;
  9.     callback();
  10.     return;
  11.   }
  12.  
  13.   var self = this;
  14.   geolocation.getCurrentPosition(
  15.     function(p) {
  16.       var addr = p.gearsAddress;
  17.       var address = addr.city + ‘, ‘ + addr.region + ‘, ‘ + addr.country;
  18.       var latitude = p.latitude;
  19.       var longitude = p.longitude;
  20.       self.geoAddress_ = address + ‘ (’ + latitude + ‘, ‘ + longitude + ‘)’;
  21.       document.getElementById(‘location’).innerHTML = self.geoAddress_;
  22.       callback();
  23.     },
  24.  
  25.     function(err) {
  26.       var msg = ‘Error retrieving your location: ‘ + err.message;
  27.       document.getElementById(‘location’).innerHTML = msg;
  28.       callback();
  29.     },
  30.    
  31.     {
  32.       enableHighAccuracy: true,
  33.       gearsRequestAddress: true,
  34.       gearsLocationProviderUrls: [‘http://www.google.com/loc/json’]
  35.     }
  36.   );
  37. }
  38.  

HTTPRequest

JAVASCRIPT:

  1.  
  2.   var req = google.gears.factory.create(‘beta.httprequest’);
  3.   req.open(‘GET’, ‘/list’);
  4.   var self = this;
  5.   req.onreadystatechange = function() {
  6.     if (req.readyState == 4) {
  7.       if (req.status == 200) {
  8.         var loadingMsg = document.getElementById(‘loadingFilesMsg’);
  9.         loadingMsg.parentNode.removeChild(loadingMsg);
  10.         self.movieList_ = eval(req.responseText);
  11.         for (var i = 0; i <self.movieList_.length; i++) {
  12.           var entry = self.movieList_[i];
  13.           // associative entry for fast lookup based on filename
  14.           self.movieList_[‘_’ + entry.filename] = entry;
  15.           var status = ‘Uploaded’
  16.           var percent = ‘100%’;
  17.          
  18.           // was this movie partially uploaded during an earlier browser
  19.           // session?
  20.           if (entry.uploaded == false && entry.blob == null) {
  21.             status = ‘Partially uploaded; re-select file to continue uploading’;
  22.             percent = Math.round((entry.bytesUploaded / entry.length) * 100) + ‘%’;
  23.           }
  24.          
  25.           // is this movie too large?
  26.           if (entry.uploaded == false && entry.length> self.MAX_FILE_SIZE) {
  27.             status = ‘File too large’;
  28.             percent = ‘N/A’;
  29.           }
  30.          
  31.           self.drawRow(entry.filename, entry.geoAddress, status, percent);
  32.         }
  33.       }
  34.      
  35.       callback();
  36.     }
  37.   }
  38.  
  39.   req.send();
  40. }
  41.  
  42. ….
  43.  
  44. // sending a piece
  45.   req.open(‘POST’, uploadURL);
  46.  
  47.   req.setRequestHeader(‘Content-Disposition’,
  48.                         ‘attachment; filename="’ + fileName + ‘"’);
  49.   req.setRequestHeader(‘Content-Type’, ‘application/octet-stream’);
  50.   req.setRequestHeader(‘Content-Range’, ‘bytes ‘ + byteRange);
  51.  
  52.  

Desktop API

JAVASCRIPT:

  1.  
  2. MovieTool.prototype.selectFiles = function() {
  3.   var desktop = google.gears.factory.create(‘beta.desktop’);
  4.   var self = this;
  5.   desktop.openFiles(
  6.     function(files) {
  7.       for (var i = 0; i <files.length; i++) {
  8.         var entry = {filename: files[i].name, uploaded: false,
  9.                             length: files[i].blob.length,
  10.                             blob: files[i].blob, bytesUploaded: 0,
  11.                             geoAddress: self.geoAddress_, uploadRetries: 0};
  12.         if (self.movieList_[‘_’ + entry.filename]) {
  13.           // was previously uploaded at an earlier browser session
  14.           var oldEntry = self.movieList_[‘_’ + entry.filename];
  15.           if (!oldEntry.uploaded) { // partial upload
  16.             oldEntry.length = entry.length;
  17.             oldEntry.blob = entry.blob;
  18.             var percent = Math.round((oldEntry.bytesUploaded / oldEntry.length) * 100);
  19.             self.updateStatus(oldEntry.filename, ‘Not uploaded’, percent + ‘%’);
  20.           } else {
  21.             self.updateStatus(oldEntry.filename, ‘Uploaded’, ‘100%’);
  22.           }
  23.         } else { // new file
  24.           // associative entry for fast lookup based on filename
  25.           self.movieList_[‘_’ + entry.filename] = entry;                   
  26.           self.movieList_.push(entry);
  27.          
  28.           var status = ‘Not uploaded’;
  29.           var percent = ‘0%’;
  30.           // is this movie too large?
  31.           if (entry.length> self.MAX_FILE_SIZE) {
  32.             status = ‘File too large’;
  33.             percent = ‘N/A’;
  34.           }
  35.          
  36.           self.drawRow(files[i].name, self.geoAddress_, status, percent);
  37.         }
  38.       }
  39.      
  40.       document.getElementById(‘movieUpload’).disabled = false;
  41.       document.getElementById(‘clearMovies’).disabled = false;
  42.     },
  43.    
  44.     { filter: [‘video/quicktime’, ‘.wmv’, ‘video/avi’] }
  45.   );
  46. }
  47.  

It turns out that YouTube has implemented a multifile upload using Gears which puts this into practice. As someone who uses YouTube a lot to upload files this is very welcome indeed, and I can’t wait to see more sites use it now the building blocks are there in a different way (can always use Flash / other controls in the past).

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blogmarks
  • del.icio.us
  • De.lirio.us
  • digg
  • NewsVine
  • YahooMyWeb

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>