forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 279
/
handlers.js
55 lines (48 loc) · 2.04 KB
/
handlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define */
define(function (require, exports, module) {
"use strict";
var Content = require("filesystem/impls/filer/lib/content");
var HTMLRewriter = require("filesystem/impls/filer/lib/HTMLRewriter");
var CSSRewriter = require("filesystem/impls/filer/lib/CSSRewriter");
var UrlCache = require("filesystem/impls/filer/UrlCache");
var Transforms = require("filesystem/impls/filer/lib/transforms");
var Path = require("filesystem/impls/filer/BracketsFiler").Path;
/**
* Process known files into Blob URLs, processing known types first
* so they are rendered and rewritten (e.g., paths->blob urls) properly.
*/
function handleFile(path, data, callback) {
var ext = Path.extname(path);
var mimeType = Content.mimeFromExt(ext);
// NOTE: we call toString() on `data` so that only utf8 data is used if a
// buffer was passed in as a parameter and should be read as utf8.
if(Content.isUTF8Encoded(ext)) {
data = data.toString();
}
function rewriteUrl() {
if(Content.isHTML(ext)) {
HTMLRewriter.rewrite(path, data, callback);
} else if(Content.isCSS(ext)) {
CSSRewriter.rewrite(path, data, function(err, css) {
if(err) {
console.error("[Handler.handleFile() Error", path, err);
callback(err);
return;
}
UrlCache.createURL(path, css, mimeType, callback);
});
} else {
UrlCache.createURL(path, data, mimeType, callback);
}
}
// If there's a transform needed for this file, do it first
Transforms.applyTransform(path, data, function(err) {
if(err) {
return callback(err);
}
rewriteUrl();
});
}
exports.handleFile = handleFile;
});