<commit_before>const mime             = require('mime');
const { query, check } = require('express-validator/check');

const models            = require('../../models');
const responseUtil      = require('./responseUtil');
const util              = require('./util');
const config            = require('../../config');
const jsonwebtoken      = require('jsonwebtoken');
const middleware        = require('../middleware');


module.exports = (app, baseUrl) => {
  var apiUrl = baseUrl + '/recordings';

  const downloadRecording = util.multipartDownload('recording', (request, data, key) => {
    var recording = models.Recording.build(data, {
      fields: models.Recording.apiSettableFields,
    });
    recording.set('rawFileKey', key);
    recording.set('rawMimeType', guessRawMimeType(data.type, data.filename));
    recording.set('DeviceId', request.device.id);
    recording.set('GroupId', request.device.GroupId);
    recording.set('processingState', models.Recording.processingStates[data.type][0]);
    if (typeof request.device.public === 'boolean') {
      recording.set('public', request.device.public);
    }
    return recording;
  });

  /**
   * @apiDefine RecordingParams
   *
   * @apiParam {JSON} data Metadata about the recording.   Valid tags are:
   * <ul>
   * <li>(REQUIRED) type: must be 'thermalRaw'
   * <li>duration
   * <li>recordingDateTime
   * <li>location
   * <li>version
   * <li>batteryCharging
   * <li>batteryLevel
   * <li>airplaneModeOn
   * <li>additionalMetadata
   * <li>comment
   * </ul>
   * @apiParam {File} file Recording file to upload
   */

  /**
   * @api {post} /api/v1/recordings Add a new recording.
   * @apiName PostRecording
   * @apiGroup Recordings
   * @apiDescription Uploads a device's own raw thermal video to the server.  It currently
   * supports raw thermal video but will eventually support all recording types.
   *
   * @apiUse V1DeviceAuthorizationHeader
   *
   * @apiUse RecordingParams
   *
   * @apiUse V1ResponseSuccess
   * @apiSuccess {Number} recordingId ID of the recording.
   * @apiuse V1ResponseError
   */
  app.post(
    apiUrl,
    [
      middleware.authenticateDevice,
    ],
    middleware.requestWrapper(
      downloadRecording
    )
  );

  /**
   * @api {post} /api/v1/recordings/:devicename Add a new recording on behalf of device
   * @apiName PostRecordingOnBehalf
   * @apiGroup Recordings
   * @apiDescription Called by a user to upload raw thermal video on behalf of a device.
   * It currently supports raw thermal video but will eventually support all
   * recording types.
   *
   * @apiUse V1UserAuthorizationHeader
   *
   * @apiUse RecordingParams
   *
   * @apiUse V1ResponseSuccess
   * @apiSuccess {Number} recordingId ID of the recording.
   * @apiuse V1ResponseError
   */
  app.post(
    apiUrl + "/:devicename",
    [
      middleware.authenticateUser,
      middleware.getDeviceByName,
    ],
    middleware.requestWrapper(async (request, response) => {
      var device = request.body["device"];<commit_msg>Added condition in help that user must have permissions for device.<commit_after>const mime             = require('mime');
const { query, check } = require('express-validator/check');

const models            = require('../../models');
const responseUtil      = require('./responseUtil');
const util              = require('./util');
const config            = require('../../config');
const jsonwebtoken      = require('jsonwebtoken');
const middleware        = require('../middleware');


module.exports = (app, baseUrl) => {
  var apiUrl = baseUrl + '/recordings';

  const downloadRecording = util.multipartDownload('recording', (request, data, key) => {
    var recording = models.Recording.build(data, {
      fields: models.Recording.apiSettableFields,
    });
    recording.set('rawFileKey', key);
    recording.set('rawMimeType', guessRawMimeType(data.type, data.filename));
    recording.set('DeviceId', request.device.id);
    recording.set('GroupId', request.device.GroupId);
    recording.set('processingState', models.Recording.processingStates[data.type][0]);
    if (typeof request.device.public === 'boolean') {
      recording.set('public', request.device.public);
    }
    return recording;
  });

  /**
   * @apiDefine RecordingParams
   *
   * @apiParam {JSON} data Metadata about the recording.   Valid tags are:
   * <ul>
   * <li>(REQUIRED) type: must be 'thermalRaw'
   * <li>duration
   * <li>recordingDateTime
   * <li>location
   * <li>version
   * <li>batteryCharging
   * <li>batteryLevel
   * <li>airplaneModeOn
   * <li>additionalMetadata
   * <li>comment
   * </ul>
   * @apiParam {File} file Recording file to upload
   */

  /**
   * @api {post} /api/v1/recordings Add a new recording.
   * @apiName PostRecording
   * @apiGroup Recordings
   * @apiDescription Uploads a device's own raw thermal video to the server.  It currently
   * supports raw thermal video but will eventually support all recording types.
   *
   * @apiUse V1DeviceAuthorizationHeader
   *
   * @apiUse RecordingParams
   *
   * @apiUse V1ResponseSuccess
   * @apiSuccess {Number} recordingId ID of the recording.
   * @apiuse V1ResponseError
   */
  app.post(
    apiUrl,
    [
      middleware.authenticateDevice,
    ],
    middleware.requestWrapper(
      downloadRecording
    )
  );

  /**
   * @api {post} /api/v1/recordings/:devicename Add a new recording on behalf of device
   * @apiName PostRecordingOnBehalf
   * @apiGroup Recordings
   * @apiDescription Called by a user to upload raw thermal video on behalf of a device.
   * The user must have permission to view videos from the device or the call will return an
   * error.  It currently supports raw thermal video but will eventually support all
   * recording types.
   *
   * @apiUse V1UserAuthorizationHeader
   *
   * @apiUse RecordingParams
   *
   * @apiUse V1ResponseSuccess
   * @apiSuccess {Number} recordingId ID of the recording.
   * @apiuse V1ResponseError
   */
  app.post(
    apiUrl + "/:devicename",
    [
      middleware.authenticateUser,
      middleware.getDeviceByName,
    ],
    middleware.requestWrapper(async (request, response) => {
      var device = request.body["device"];
{'fraction_non_alphanumeric': 0.12050330698499757, 'fraction_numerical': 0.002903694144216809, 'mean_word_length': 3.144385026737968, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1413610', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1845, 'n_words': 469}