Permalink
Browse files

Corrections to content negotiations with alternates, and added saveFi…

…le and loadFile handlers
  • Loading branch information...
1 parent 7aaff76 commit 28c55ef807bea8715a841756136f9757841e2298 @kriszyp kriszyp committed Aug 12, 2010
Showing with 45 additions and 27 deletions.
  1. +45 −27 lib/media.js
View
@@ -33,26 +33,7 @@ Media.optimumMedia = function(source, acceptTypeHeader){
if("*/*" == type){
for(var i in Media.instances){
- checkMedia(Media.instances[i], parameters);
- }
- // use alternates per http://tools.ietf.org/html/draft-ietf-http-alternates-01
- var alternates;
- if(source && typeof source.getMetadata === "function" && (alternates = source.getMetadata().alternates)){
- if(!(alternates instanceof Array)){
- alternates = [alternates];
- }
- alternates.forEach(function(alternate){
- var metadata = alternate.getMetadata();
- checkMedia({
- serialize: function(){
- return alternate;
- },
- getQuality: function(){
- return +(metadata.quality || 1);
- },
- mediaType: metadata["content-type"]
- }, parameters);
- });
+ checkMedia(Media.instances[i]);
}
/* for persvr compatibility:
for(var i in source){
@@ -64,16 +45,37 @@ Media.optimumMedia = function(source, acceptTypeHeader){
else{
var media = Media.instances[type];
if(media){
- checkMedia(media, parameters);
+ checkMedia(media);
}
/* for persvr compatibility:
media = source && source["representation:" + type];
if(media){
checkMedia(media);
}*/
}
+ // use alternates per http://tools.ietf.org/html/draft-ietf-http-alternates-01
+ var alternates;
+ if(source && ((typeof source.getMetadata === "function" && (alternates = source.getMetadata().alternates)) || (source._metadata && (alternates = source._metadata.alternates)))){
+ if(!(alternates instanceof Array)){
+ alternates = [alternates];
+ }
+ alternates.forEach(function(alternate){
+ var mediaType = alternate["content-type"];
+ if("*/*" == type || mediaType == type){;
+ checkMedia({
+ serialize: function(){
+ return exports.loadFile(alternate);
+ },
+ getQuality: function(){
+ return +(alternate.q || 1);
+ },
+ mediaType: mediaType
+ });
+ }
+ });
+ }
}
- function checkMedia(media, parameters){
+ function checkMedia(media){
var quality = clientQuality * media.getQuality(source);
if(quality > bestQuality){
bestMedia = media;
@@ -119,10 +121,26 @@ exports.setFileModel = function(value){
};
exports.fileSchema = {};
-exports.onFile = function(file, directives){
- file.isFile = true;
- return when(exports.getFileModel().put(file, directives), function(file){
- file = file.id || file;
- return {$ref:file};
+exports.saveFile = function(file, directives){
+ var fileObject = {};
+ for(var i in file){
+ if(typeof file[i] == "string"){
+ fileObject[i] = file[i];
+ }
+ }
+ return when(exports.getFileModel().put(file, directives), function(result){
+ var id = result.id || result;
+ result = {id: id};
+ fileObject.id = id;
+ Object.defineProperty(result, "getMetadata",{
+ value: function(){
+ return fileObject;
+ },
+ enumerable: false
+ });
+ return result;
});
};
+exports.loadFile = function(file, directives){
+ return exports.getFileModel().get(file.id, directives);
+};

0 comments on commit 28c55ef

Please sign in to comment.