Ejemplo n.º 1
0
function addStats (resourceGraph, reqUri, stats, filename) {
  resourceGraph.add(
    resourceGraph.sym(reqUri),
    ns.stat('mtime'),  // Deprecate?
    stats.mtime.getTime() / 1000)

  resourceGraph.add(
    resourceGraph.sym(reqUri),
    ns.dct('modified'),
    stats.mtime) // An actual datetime value from a Date object

  resourceGraph.add(
    resourceGraph.sym(reqUri),
    ns.stat('size'),
    stats.size)

  if (mime.lookup(filename)) { // Is the file has a well-known type,
    let type = 'http://www.w3.org/ns/iana/media-types/' + mime.lookup(filename) + '#Resource'
    resourceGraph.add(
      resourceGraph.sym(reqUri),
      ns.rdf('type'), // convert MIME type to RDF
      resourceGraph.sym(type)
    )
  }
}
Ejemplo n.º 2
0
function* pipe(info, attachment) {
  this.type = mime.lookup(info.url);

  if (typeof info.size === 'number' && info.size > 0) {
    this.length = info.size;
  }

  var type = mime.lookup(info.url);
  if (/\.(txt\.asc|txt\.gpg|tab)$/.test(info.name) ||
     (/RELEASES$/.test(info.name))) {
    type = 'text/plain';
  }
  debug('pipe %j, attachment: %s, type: %s', info, attachment, type);
  if (type) {
    this.type = type;
  }

  var etag = info.md5 || info.sha1;
  if (etag) {
    this.etag = etag;
  }

  if (attachment) {
    this.attachment(info.name);
  }

  return this.body = yield* downloadAsReadStream(info.url);
}
Ejemplo n.º 3
0
FormData.prototype._getContentType = function (value, options) {

  // use custom content-type above all
  var contentType = options.contentType;

  // or try `path` from fs-, request- streams
  if (!contentType && value.path) {
    contentType = mime.lookup(value.path);
  }

  // or if it's http-reponse
  if (!contentType && value.readable && value.hasOwnProperty('httpVersion')) {
    contentType = value.headers['content-type'];
  }

  // or guess it from the filename
  if (!contentType && options.filename) {
    contentType = mime.lookup(options.filename);
  }

  // fallback to the default content type if `value` is not simple value
  if (!contentType && typeof value == 'object') {
    contentType = FormData.DEFAULT_CONTENT_TYPE;
  }

  return contentType;
};
Ejemplo n.º 4
0
    req.on('end', function() {
      // check for the fields' traces

      // 1st field : my_field
      t.ok( data.indexOf('form-data; name="my_field"') != -1 )
      t.ok( data.indexOf(multipartFormData.my_field) != -1 )

      // 2nd field : my_buffer
      t.ok( data.indexOf('form-data; name="my_buffer"') != -1 )
      t.ok( data.indexOf(multipartFormData.my_buffer) != -1 )

      // 3rd field : my_file
      t.ok( data.indexOf('form-data; name="my_file"') != -1 )
      t.ok( data.indexOf('; filename="'+path.basename(multipartFormData.my_file.path)+'"') != -1 )
      // check for unicycle.jpg traces
      t.ok( data.indexOf('2005:06:21 01:44:12') != -1 )
      t.ok( data.indexOf('Content-Type: '+mime.lookup(multipartFormData.my_file.path) ) != -1 )

      // 4th field : remote_file
      t.ok( data.indexOf('form-data; name="remote_file"') != -1 )
      t.ok( data.indexOf('; filename="'+path.basename(multipartFormData.remote_file.path)+'"') != -1 )
      // check for http://nodejs.org/images/logo.png traces
      t.ok( data.indexOf('ImageReady') != -1 )
      t.ok( data.indexOf('Content-Type: '+mime.lookup(remoteFile) ) != -1 )

      res.writeHead(200)
      res.end('done')
    })
Ejemplo n.º 5
0
  /**  
   * Handles invoking compilers independent of caching
   *
   * @private
   */
  async compileUncached(filePath, hashInfo, compiler) {
    let inputMimeType = mimeTypes.lookup(filePath);
    
    if (hashInfo.isFileBinary) {
      return {
        binaryData: hashInfo.binaryData || await pfs.readFile(filePath),
        mimeType: inputMimeType,
        dependentFiles: []
      };
    }
    
    let ctx = {};
    let code = hashInfo.sourceCode || await pfs.readFile(filePath, 'utf8');

    if (!(await compiler.shouldCompileFile(code, ctx))) {
      d(`Compiler returned false for shouldCompileFile: ${filePath}`);
      return { code, mimeType: mimeTypes.lookup(filePath), dependentFiles: [] };
    }

    let dependentFiles = await compiler.determineDependentFiles(code, filePath, ctx);

    d(`Using compiler options: ${JSON.stringify(compiler.compilerOptions)}`);
    let result = await compiler.compile(code, filePath, ctx);

    let shouldInlineHtmlify = 
      inputMimeType !== 'text/html' &&
      result.mimeType === 'text/html';
    
    let isPassthrough = 
      result.mimeType === 'text/plain' || 
      !result.mimeType || 
      CompilerHost.shouldPassthrough(hashInfo);
      
    if ((finalForms[result.mimeType] && !shouldInlineHtmlify) || isPassthrough) {
      // Got something we can use in-browser, let's return it
      return _.assign(result, {dependentFiles});
    } else {
      d(`Recursively compiling result of ${filePath} with non-final MIME type ${result.mimeType}, input was ${inputMimeType}`);

      hashInfo = _.assign({ sourceCode: result.code, mimeType: result.mimeType }, hashInfo);
      compiler = this.compilersByMimeType[result.mimeType || '__lolnothere'];

      if (!compiler) {
        d(`Recursive compile failed - intermediate result: ${JSON.stringify(result)}`);

        throw new Error(`Compiling ${filePath} resulted in a MIME type of ${result.mimeType}, which we don't know how to handle`);
      }

      return await this.compileUncached(
        `${filePath}.${mimeTypes.extension(result.mimeType || 'txt')}`, 
        hashInfo, compiler);
    }
  }
Ejemplo n.º 6
0
// fallback (magic?: Object, filePath: String) => String
function fallback (magic, filePath) {
  if (!magic || !magic.mime) {
    // first 262 bytes told us nothing
    // detect using filename extension
    return mime.lookup(filePath) || 'application/octet-stream';
  }
  if (GENERIC_TYPES.indexOf(magic.mime) !== -1) {
    // first 262 bytes told us nothing useful
    // detect using filename extension
    return mime.lookup(filePath) || magic.mime;
  }
  // use the result from the first 262 bytes
  return magic.mime;
}
Ejemplo n.º 7
0
function convertItem(item, data) {
    var item_values = [
        { title:        { _cdata: item.title } }
    ];
    ifTruePush(item.description, item_values, { description:  { _cdata: item.description } });
    ifTruePush(item.url, item_values, { link: item.url });
    ifTruePush(item.link || item.guid || item.title, item_values, { guid:         [ { _attr: { isPermaLink: !item.guid && !!item.url } }, item.guid || item.url || item.title ]  });

    if (item.categories) {
        item.categories.forEach(function(category) {
            ifTruePush(category, item_values, { category: { _cdata: category } });
        });
    }

    ifTruePush(item.author || data.author, item_values, { 'dc:creator': { _cdata: item.author || data.author } });
    ifTruePush(item.date, item_values, { pubDate:      new Date(item.date).toGMTString() });

    //Set GeoRSS to true if lat and long are set
    data.geoRSS = data.geoRSS || (item.lat && item.long);
    ifTruePush(item.lat, item_values, {'geo:lat': item.lat});
    ifTruePush(item.long, item_values, {'geo:long': item.long});

    if( item.enclosure && item.enclosure.url) {
        if( item.enclosure.file ) {
            item_values.push({
                enclosure : {
                    _attr : {
                        url : item.enclosure.url,
                        length : item.enclosure.size || getSize(item.enclosure.file),
                        type : item.enclosure.type || mime.lookup(item.enclosure.file)
                    }
                }
            });
        } else {
            item_values.push({
                enclosure : {
                    _attr : {
                        url : item.enclosure.url,
                        length : item.enclosure.size || 0,
                        type : item.enclosure.type || mime.lookup(item.enclosure.url)
                    }
                }
            });
        }
    }

    ifTruePushArray(item.custom_elements, item_values, item.custom_elements);

    return { item: item_values };
}
Ejemplo n.º 8
0
Cache.prototype.cachingEnabled = function (req) {
  // Check it is not a json view
  var query = url.parse(req.url, true).query
  if (query.json && query.json !== 'false') return false

  // Disable cache for debug mode
  if (config.get('debug')) return false

  // if it's in the endpoint and caching is enabled
  var endpoint = this.getEndpoint(req)

  if (endpoint) {
    this.options.cache =
      typeof endpoint.page.settings.cache !== 'undefined'
        ? endpoint.page.settings.cache
        : this.enabled

    return this.enabled && this.options.cache
  } else {
    // Otherwise it might be in the public folder
    var file = url.parse(req.url).pathname

    return compressible(mime.lookup(file))
  }
}
			return filenames.map(function(filename) {
				return {
					filename: filename,
					localPath: pathModule.join(path, filename),
					headers: { 'content-type': addCharsetToContentType(mime.lookup(filename)) },
				}
			})
Ejemplo n.º 10
0
  _rangeRequestMiddleware(
  req,
  res,
  data,
  assetPath)
  {
    if (req.headers && req.headers.range) {var _req$headers$range$re =
      req.headers.range.
      replace(/bytes=/, '').
      split('-'),_req$headers$range$re2 = _slicedToArray(_req$headers$range$re, 2);const rangeStart = _req$headers$range$re2[0],rangeEnd = _req$headers$range$re2[1];
      const dataStart = parseInt(rangeStart, 10);
      const dataEnd = rangeEnd ? parseInt(rangeEnd, 10) : data.length - 1;
      const chunksize = dataEnd - dataStart + 1;

      res.writeHead(206, {
        'Accept-Ranges': 'bytes',
        'Content-Length': chunksize.toString(),
        'Content-Range': `bytes ${dataStart}-${dataEnd}/${data.length}`,
        'Content-Type': mime.lookup(path.basename(assetPath[1])) });


      return data.slice(dataStart, dataEnd + 1);
    }

    return data;
  }
Ejemplo n.º 11
0
      fs.readFile(filepath, function(err, data) {
        if (err) { return reject(err); }

        // file extension
        var ext = path.extname(filepath);

        // bucket & key
        var key = sprintf('%d/photo/%d%s', user_id, moment().valueOf(), ext);
        var url = sprintf('%s/%s/%s', api.config.aws.s3.host, bucket, key);

        // params
        var params = {
          Bucket      : bucket,
          Key         : key,
          Body        : data,
          ContentType : mime.lookup(filepath),
          ACL         : 'public-read',
          StorageClass: 'STANDARD',       // STANDARD | REDUCED_REDUNDANCY
          CacheControl: 'max-age=86400',
        };

        // upload file to s3
        S3.putObject(params, function(err, data) {
          if (err) { return reject(err); }

          return resolve(url);
        });
      });
Ejemplo n.º 12
0
 fs.readFile(result, function (err1, data) {
   member.setAvatarData({
     image: 'data:' + mimetypes.lookup(result) + ';base64,' + new Buffer(data).toString('base64'),
     hasNoImage: false
   });
   callback(err1);
 });
 .on('file', function(name, file) {
   var field = FIELDS.shift();
   assert.strictEqual(name, field.name);
   // http response doesn't have path property
   assert.strictEqual(file.name, path.basename(field.value.path || remoteFile));
   assert.strictEqual(file.type, mime.lookup(file.name));
 })
Ejemplo n.º 14
0
function test(basename) {
  var m = re_test.exec(basename)
  if (!m) return false

  var ext = m[1]
  if (typeis(mime.lookup(ext), 'text/*')) return ext
}
Ejemplo n.º 15
0
}).on('all', (event, original_path) => { // in the event of any watched activity...
    var destination_path = original_path.replace('arcdown', 'public');
    if (event == "addDir") { // if adding a dir create a new matching dir in public if it doesn't already exist.
        if (!exists(destination_path)) {
            fs.mkdir(destination_path, (err) => {
                symlink(public_path + '/css', destination_path + '/css', 'dir');
            });
        }
    } else if (event == "add" || event == "change") {
        if (mime.lookup(original_path) == 'text/x-markdown') {
            fs.readFile(original_path, 'utf8', (err, data) => { // read the md file convert and write it to public folder
                data = combine(data);
                destination_path = destination_path.replace(/\.md$/, '.html');
                writeFile(destination_path, data);
            });
        } else {
            fs.readFile(original_path, (err, data) => { // read the md file convert and write it to public folder
                writeFile(destination_path, data);
            });
        }
    } else if (event == "unlink") {
        rm_file(destination_path);
    } else if (event == "unlinkDir") {
        rm_dir(destination_path);
    } else {
        console.log('I did something that the program didn\'t plan on ' + event, original_path);
    }
});
Ejemplo n.º 16
0
      .then((type) => {
        if (type === `application/x-msi`) {
          return mime.lookup(file.name);
        }

        return type;
      });
Ejemplo n.º 17
0
            require("fs").writeFile(upload, decodedImage, function(err) {
                if (err) return next(err);

                var dimensions  = sizeOf(upload),
                    stats       = fs.statSync(upload),
                    extension   = path.extname(upload).replace('.', ''),
                    type        = mime.lookup(upload);

                type = type.split('/')[0];

                var meta = { width: dimensions.width, height: dimensions.height, size: stats['size'], extension: extension, type: type };
                _.extend(req.body, meta);

                Media.findOne({ name: req.body.name }, function(err, media) {
                    if (err) next(err);
                    if (media) return res.status(500).json({ code: 500, message: 'Name already in use' });

                    var media = new Media(req.body);

                    media.save(function(err, media) {
                        if (err) {
                            return next(err);
                        }
                        else {
                            res.status(200).json(media);
                        }
                    });
                });
            });
Ejemplo n.º 18
0
  fullCompileSync(filePath) {
    d(`Compiling ${filePath}`);

    let hashInfo = this.fileChangeCache.getHashForPathSync(filePath);
    let type = mimeTypes.lookup(filePath);
    
    if (hashInfo.isInNodeModules) {
      let code = hashInfo.sourceCode || fs.readFileSync(filePath, 'utf8');
      return { code, mimeType: type };
    }

    let compiler = CompilerHost.shouldPassthrough(hashInfo) ?
      this.getPassthroughCompiler() :
      this.compilersByMimeType[type || '__lolnothere'];

    if (!compiler) {
      d(`Falling back to passthrough compiler for ${filePath}`);
      compiler = this.fallbackCompiler;
    }

    if (!compiler) {
      throw new Error(`Couldn't find a compiler for ${filePath}`);
    }

    let cache = this.cachesForCompilers.get(compiler);
    return cache.getOrFetchSync(
      filePath,
      (filePath, hashInfo) => this.compileUncachedSync(filePath, hashInfo, compiler));
  }
Ejemplo n.º 19
0
  filetype : function (req,res) {
    var mime = require('mime-types');
    var reqfast = require('req-fast');
    var URI = encodeURI(req.param('uri'));

    var mimetype = mime.lookup(URI);
    if(mimetype == 'application/x-msdownload' && URI.indexOf('.com') )
      mimetype = false;

    if(!mimetype)
    {
      reqfast(URI, function(err, resp){
        if(err){
          return res.serverError(err);
        }
        if(resp && resp.statusCode == 200)
        {
          return res.json({
            type:'text/html'
          });
        }
      });
    }
    else {
      return res.json({
        type:mimetype
      });
    }
  },
Ejemplo n.º 20
0
  /**  
   * Handles compilation in read-only mode
   *
   * @private
   */   
  async compileReadOnly(filePath) {
    // We guarantee that node_modules are always shipped directly
    let type = mimeTypes.lookup(filePath);
    if (FileChangedCache.isInNodeModules(filePath)) {
      return { 
        mimeType: type || 'application/javascript',
        code: await pfs.readFile(filePath, 'utf8') 
      };    
    }
    
    let hashInfo = await this.fileChangeCache.getHashForPath(filePath);

    // NB: Here, we're basically only using the compiler here to find
    // the appropriate CompileCache
    let compiler = CompilerHost.shouldPassthrough(hashInfo) ?
      this.getPassthroughCompiler() :
      this.compilersByMimeType[type || '__lolnothere'];

    if (!compiler) { 
      compiler = this.fallbackCompiler;

      let { code, binaryData, mimeType } = await compiler.get(filePath);
      return { code: code || binaryData, mimeType };
    }

    let cache = this.cachesForCompilers.get(compiler);
    let {code, binaryData, mimeType} = await cache.get(filePath);

    code = code || binaryData;
    if (!code || !mimeType) {
      throw new Error(`Asked to compile ${filePath} in production, is this file not precompiled?`);
    }

    return { code, mimeType };
  }
Ejemplo n.º 21
0
  return Promise.map(genres, ({ assetsPaths, tag }) => {
    const promises = [];

    // eslint-disable-next-line no-restricted-syntax, guard-for-in
    for (const asset in assetsPaths) {
      const assetPath = assetsPaths[asset];
      const file = path.basename(tag[asset]);

      const opts = {
        destination: `${argv.prefix}/${tag.id}/${file}`,
        resumable: false,
        public: true,
        metadata: {
          contentType: mime.lookup(assetPath) || 'application/octet-stream',
          cacheControl: 'public, max-age=31536000',
        },
      };

      promises.push(
        bucket.uploadAsync(assetPath, opts).tap(() => {
          console.info('uploaded %s to https://%s/%s', assetPath, argv.cdn, opts.destination);
        })
      );
    }

    return Promise.all(promises);
  });
Ejemplo n.º 22
0
  isFileIdenticaltoS3File(filename, Key, (err, isIdentical) => {
    if (err) throw err;
    if (isIdentical) return console.log(chalk.gray(`${fullS3Path}: Has not changed`));

    const Body = fs.createReadStream(filename);
    const ContentType = mime.lookup(filename) || 'application/octet-stream';
    const ACL = opts.isPublicFile ? 'public-read' : 'private';

    const params = {
      ACL,
      Body,
      ContentType,
      Key
    };

    if (opts.shouldCache) {
      const cacheParams = cacheLookup(ContentType);
      if (cacheParams) params.CacheControl = cacheParams.control;
    }

    s3.putObject(params, (err, data) => {
      if (err) throw err;
      console.log(chalk.green(`Uploaded: ${fullS3Path}`));
    });
  });
Ejemplo n.º 23
0
			async.each(Object.keys(files), function(file, callback) {
				param.Key = file;
				param.Body = files[file].contents;
				param.ContentType = mime.lookup(file);
				s3.putObject(param, callback);
				debug("writing file: ", file);
			}, function(err) {
Ejemplo n.º 24
0
			this.for(availableMockResponses, function (mockItem) {

				var methodStore = this.getMethodStore(itemPath);
				var validationResult;
				var nameMockItem = mockItem.file.replace('.json', '');

				if (nameMockItem.search(/^\./) >= 0) {
					return;
				}

				if (nameMockItem.search(/\.headers$/) >= 0) {
					return;
				}

				try {
					validationResult = methodStore.validation[nameMockItem].counter;
				} catch (err) {
					validationResult = undefined;
				}

				mockItem.isSelected = (selected === nameMockItem);
				mockItem.name = mockItem.file;
				mockItem.mime = mime.lookup(mockItem.file);
				mockItem.isValidated = (validationResult !== undefined);

				if (mockItem.isValidated) {
					mockItem.isValid = (validationResult < 1);
					mockItem.inValidCounter = validationResult;
				}

				availableMockResponsesOut.push(mockItem);
			}.bind(this));
Ejemplo n.º 25
0
  return function (req, res, next) {
    var match = req.url.match(/\/__\/env(.json|.js)/);
    if (!match) return next();
    
    var envConfig = _.merge(env.envConfig(settings).env || {}, localEnv || {});
    var payload;
    var payloadType;
    
    // TODO: test this
    // current build/release status data
    if (req.config && req.config.version) {
      envConfig.__release = {
        build_id: req.config.buildId,
        release: req.config.version,
        timestamp: req.config.timestamp
      };
    }

    if (match[1] === '.json') {
      payload = JSON.stringify(envConfig);
      payloadType = mime.lookup('.json');
    }
    else {
      payload = template.replace("{{ENV}}", JSON.stringify(envConfig));
      payloadType = 'text/javascript';
    }
    
    res.writeHead(200, {
      'Content-Type': payloadType,
      'Content-Length': payload.length
    });
    
    res.end(payload);
  };
Ejemplo n.º 26
0
		var doGzipRename = function (object, options) {
			var lastDot = object.src.lastIndexOf('.')

			if (object.src.substr(lastDot) === '.gz') {

				var originalPath = object.src.substr(0, lastDot)

				object.params = _.defaults({
					ContentType: mime.contentType(mime.lookup(originalPath) || "application/octet-stream"),
					ContentEncoding: 'gzip'
				}, object.params || {})

				if (options.gzipRename && object.src.match(/\.[^.]+\.gz$/)) {

					if (options.gzipRename === 'ext') {
						object.dest = object.dest.replace(/\.gz$/, '')
					}
					else if (options.gzipRename === 'gz') {
						object.dest = object.dest.replace(/\.[^.]+\.gz$/, '.gz')
					}
					else if (options.gzipRename === 'swap') {
						object.dest = object.dest.replace(/(\.[^.]+)\.gz$/, '.gz$1')
					}
				}
			}
		};
FormData.prototype._multiPartHeader = function(field, value, options) {
  var boundary = this.getBoundary();
  var header = '';

  // custom header specified (as string)?
  // it becomes responsible for boundary
  // (e.g. to handle extra CRLFs on .NET servers)
  if (options.header != null) {
    header = options.header;
  } else {
    header += '--' + boundary + FormData.LINE_BREAK +
      'Content-Disposition: form-data; name="' + field + '"';

    // fs- and request- streams have path property
    // or use custom filename and/or contentType
    // TODO: Use request's response mime-type
    if (options.filename || value.path) {
      header +=
        '; filename="' + path.basename(options.filename || value.path) + '"' + FormData.LINE_BREAK +
        'Content-Type: ' +  (options.contentType || mime.lookup(options.filename || value.path));

    // http response has not
    } else if (value.readable && value.hasOwnProperty('httpVersion')) {
      header +=
        '; filename="' + path.basename(value.client._httpMessage.path) + '"' + FormData.LINE_BREAK +
        'Content-Type: ' + value.headers['content-type'];
    }

    header += FormData.LINE_BREAK + FormData.LINE_BREAK;
  }

  return header;
};
Ejemplo n.º 28
0
 var concatStream = concat(function handleFileBuffer (buff) {
   // https://github.com/scijs/get-pixels/blob/2e8766f62a9043d74a4b1047294a25fea5b2eacf/node-pixels.js#L179
   if (buff.length === 0) {
     return cb(new Error('Expected image "' + file.path + '" to not be empty but it was'));
   }
   getPixels(buff, mime.lookup(file.path), cb);
 });
Ejemplo n.º 29
0
  self.on('pipe', function (src) {
    if (self.ntick && self._started) {
      throw new Error('You cannot pipe to this stream after the outbound request has started.')
    }
    self.src = src
    if (isReadStream(src)) {
      if (!self.hasHeader('content-type')) {
        self.setHeader('content-type', mime.lookup(src.path))
      }
    } else {
      if (src.headers) {
        for (var i in src.headers) {
          if (!self.hasHeader(i)) {
            self.setHeader(i, src.headers[i])
          }
        }
      }
      if (self._json && !self.hasHeader('content-type')) {
        self.setHeader('content-type', 'application/json')
      }
      if (src.method && !self.explicitMethod) {
        self.method = src.method
      }
    }

    // self.on('pipe', function () {
    //   console.error('You have already piped to this stream. Pipeing twice is likely to break the request.')
    // })
  })
Ejemplo n.º 30
0
 .then(function () {
     return {
         mime: mime.lookup(disFile),
         path: disFile,
         contentType: mime.contentType(path.extname(disFile))
     }
 })