<commit_before>/*
 * (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.openvidu.server.recording.service;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.openvidu.client.OpenViduException;
import io.openvidu.client.OpenViduException.Code;
import io.openvidu.java.client.RecordingLayout;
import io.openvidu.java.client.RecordingProperties;
import io.openvidu.server.config.OpenviduConfig;
import io.openvidu.server.core.EndReason;
import io.openvidu.server.core.Session;
import io.openvidu.server.recording.Recording;
import io.openvidu.server.recording.RecordingDownloader;
import io.openvidu.server.utils.CommandExecutor;
import io.openvidu.server.utils.CustomFileManager;

public abstract class RecordingService {

	private static final Logger log = LoggerFactory.getLogger(RecordingService.class);

	protected OpenviduConfig openviduConfig;
	protected RecordingManager recordingManager;
	protected RecordingDownloader recordingDownloader;
	protected CustomFileManager fileWriter = new CustomFileManager();

	RecordingService(RecordingManager recordingManager, RecordingDownloader recordingDownloader,
			OpenviduConfig openviduConfig) {
		this.recordingManager = recordingManager;
		this.recordingDownloader = recordingDownloader;
		this.openviduConfig = openviduConfig;
	}

	public abstract Recording startRecording(Session session, RecordingProperties properties) throws OpenViduException;

	public abstract Recording stopRecording(Session session, Recording recording, EndReason reason);

	/**
	 * Generates metadata recording file (".recording.RECORDING_ID" JSON file to
	 * store Recording entity)
	 */
	protected void generateRecordingMetadataFile(Recording recording) {
		String folder = this.openviduConfig.getOpenViduRecordingPath() + recording.getId();
		boolean newFolderCreated = this.fileWriter.createFolderIfNotExists(folder);

		if (newFolderCreated) {
			log.warn(
					"New folder {} created. This means A) Cluster mode is enabled B) The recording started for a session with no publishers or C) No media type compatible publishers",
					folder);
		} else {
			log.info("Folder {} already existed. Some publisher is already being recorded", folder);
		}

		String filePath = this.openviduConfig.getOpenViduRecordingPath() + recording.getId() + "/"
				+ RecordingManager.RECORDING_ENTITY_FILE + recording.getId();
		String text = recording.toJson().toString();
		this.fileWriter.createAndWriteFile(filePath, text);
		log.info("Generated recording metadata file at {}", filePath);
	}

	/**
	 * Update and overwrites metadata recording file to set it in "processing"
	 * status. Recording size and duration will remain as 0
	 * 
	 * @return updated Recording object
	 */
	protected Recording sealRecordingMetadataFileAsProcessing(Recording recording) {

		log.info("Recording {} is processing", recording.getId());

		final String entityFile = this.openviduConfig.getOpenViduRecordingPath() + recording.getId() + "/"
				+ RecordingManager.RECORDING_ENTITY_FILE + recording.getId();
		return this.sealRecordingMetadataFile(recording, 0, 0, io.openvidu.java.client.Recording.Status.processing,
				entityFile);
	}

	/**
	 * Update and overwrites metadata recording file to set it in "stopped" (or
	 * "failed") status
	 * 
	 * @return updated Recording object
	 */
	protected Recording sealRecordingMetadataFileAsStopped(Recording recording, long size, double duration,
			String metadataFilePath) {

		log.info("Recording {} is processing", recording.getId());

		io.openvidu.java.client.Recording.Status status = io.openvidu.java.client.Recording.Status.failed
				.equals(recording.getStatus()) ? io.openvidu.java.client.Recording.Status.failed
						: io.openvidu.java.client.Recording.Status.stopped;

		final String entityFile = this.openviduConfig.getOpenViduRecordingPath() + recording.getId() + "/"
				+ RecordingManager.RECORDING_ENTITY_FILE + recording.getId();
		return this.sealRecordingMetadataFile(recording, size, duration, status, entityFile);
	}
<commit_msg>openvidu-server: RecordingService seal metadata log update<commit_after>/*
 * (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.openvidu.server.recording.service;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.openvidu.client.OpenViduException;
import io.openvidu.client.OpenViduException.Code;
import io.openvidu.java.client.RecordingLayout;
import io.openvidu.java.client.RecordingProperties;
import io.openvidu.server.config.OpenviduConfig;
import io.openvidu.server.core.EndReason;
import io.openvidu.server.core.Session;
import io.openvidu.server.recording.Recording;
import io.openvidu.server.recording.RecordingDownloader;
import io.openvidu.server.utils.CommandExecutor;
import io.openvidu.server.utils.CustomFileManager;

public abstract class RecordingService {

	private static final Logger log = LoggerFactory.getLogger(RecordingService.class);

	protected OpenviduConfig openviduConfig;
	protected RecordingManager recordingManager;
	protected RecordingDownloader recordingDownloader;
	protected CustomFileManager fileWriter = new CustomFileManager();

	RecordingService(RecordingManager recordingManager, RecordingDownloader recordingDownloader,
			OpenviduConfig openviduConfig) {
		this.recordingManager = recordingManager;
		this.recordingDownloader = recordingDownloader;
		this.openviduConfig = openviduConfig;
	}

	public abstract Recording startRecording(Session session, RecordingProperties properties) throws OpenViduException;

	public abstract Recording stopRecording(Session session, Recording recording, EndReason reason);

	/**
	 * Generates metadata recording file (".recording.RECORDING_ID" JSON file to
	 * store Recording entity)
	 */
	protected void generateRecordingMetadataFile(Recording recording) {
		String folder = this.openviduConfig.getOpenViduRecordingPath() + recording.getId();
		boolean newFolderCreated = this.fileWriter.createFolderIfNotExists(folder);

		if (newFolderCreated) {
			log.warn(
					"New folder {} created. This means A) Cluster mode is enabled B) The recording started for a session with no publishers or C) No media type compatible publishers",
					folder);
		} else {
			log.info("Folder {} already existed. Some publisher is already being recorded", folder);
		}

		String filePath = this.openviduConfig.getOpenViduRecordingPath() + recording.getId() + "/"
				+ RecordingManager.RECORDING_ENTITY_FILE + recording.getId();
		String text = recording.toJson().toString();
		this.fileWriter.createAndWriteFile(filePath, text);
		log.info("Generated recording metadata file at {}", filePath);
	}

	/**
	 * Update and overwrites metadata recording file to set it in "processing"
	 * status. Recording size and duration will remain as 0
	 * 
	 * @return updated Recording object
	 */
	protected Recording sealRecordingMetadataFileAsProcessing(Recording recording) {
		final String entityFile = this.openviduConfig.getOpenViduRecordingPath() + recording.getId() + "/"
				+ RecordingManager.RECORDING_ENTITY_FILE + recording.getId();
		return this.sealRecordingMetadataFile(recording, 0, 0, io.openvidu.java.client.Recording.Status.processing,
				entityFile);
	}

	/**
	 * Update and overwrites metadata recording file to set it in "stopped" (or
	 * "failed") status
	 * 
	 * @return updated Recording object
	 */
	protected Recording sealRecordingMetadataFileAsStopped(Recording recording, long size, double duration,
			String metadataFilePath) {
		io.openvidu.java.client.Recording.Status status = io.openvidu.java.client.Recording.Status.failed
				.equals(recording.getStatus()) ? io.openvidu.java.client.Recording.Status.failed
						: io.openvidu.java.client.Recording.Status.stopped;

		final String entityFile = this.openviduConfig.getOpenViduRecordingPath() + recording.getId() + "/"
				+ RecordingManager.RECORDING_ENTITY_FILE + recording.getId();
		return this.sealRecordingMetadataFile(recording, size, duration, status, entityFile);
	}

{'fraction_non_alphanumeric': 0.09030428618169921, 'fraction_numerical': 0.0037081470171229144, 'mean_word_length': 5.1917623227548955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5836018', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2568, 'n_words': 687}