<commit_before>'use strict';

/**
 * @ngdoc function
 * @name passmanApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the passmanApp
 */
angular.module('passmanApp')
	.controller('CredentialEditCtrl', ['$scope', 'VaultService', 'CredentialService', 'SettingsService', '$location', '$routeParams', 'FileService', 'EncryptService', 'TagService', 'NotificationService', 'ShareService',
		function ($scope, VaultService, CredentialService, SettingsService, $location, $routeParams, FileService, EncryptService, TagService, NotificationService, ShareService) {
			$scope.active_vault = VaultService.getActiveVault();
			if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
				if (!$scope.active_vault) {
					$location.path('/')
				}
			} else {
				if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
					var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
					VaultService.getVault(_vault).then(function (vault) {
						vault.vaultKey = angular.copy(SettingsService.getSetting('defaultVaultPass'));
						VaultService.setActiveVault(vault);
						$scope.active_vault = vault;

						$scope.pwSettings = VaultService.getVaultSetting('pwSettings',
							{
								'length': 12,
								'useUppercase': true,
								'useLowercase': true,
								'useDigits': true,
								'useSpecialChars': true,
								'minimumDigitCount': 3,
								'avoidAmbiguousCharacters': false,
								'requireEveryCharType': true,
								'generateOnCreate': true
							})
					})
				}
			}

			$scope.tabs = [{
				title: 'General',
				url: 'views/partials/forms/edit_credential/basics.html',
				color: 'blue'
			}, {
				title: 'Password',
				url: 'views/partials/forms/edit_credential/password.html',
				color: 'green'
			}, {
				title: 'Custom fields',
				url: 'views/partials/forms/edit_credential/custom_fields.html',
				color: 'orange'
			}, {
				title: 'Files',
				url: 'views/partials/forms/edit_credential/files.html',
				color: 'yellow'
			}, {
				title: 'OTP',
				url: 'views/partials/forms/edit_credential/otp.html',
				color: 'purple'
			}];


			if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
				if (!$scope.active_vault) {
					$location.path('/')
				}
			} else {
				if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
					var _vault = angular.copy(SettingsService.getSetting('defaultVault'))
					_vault.vaultKey = angular.copy(SettingsService.getSetting('defaultVaultPass'));
					VaultService.setActiveVault(_vault);
					$scope.active_vault = _vault;

				}
			}
			if ($scope.active_vault) {
				$scope.$parent.selectedVault = true;
			}
			var storedCredential = SettingsService.getSetting('edit_credential');

			if (!storedCredential) {
				CredentialService.getCredential($routeParams.credential_id).then(function (result) {
					$scope.storedCredential = CredentialService.decryptCredential(angular.copy(result));
				});
			} else {
				$scope.storedCredential = CredentialService.decryptCredential(angular.copy(storedCredential));
				$scope.storedCredential.password_repeat = angular.copy($scope.storedCredential.password);
				$scope.storedCredential.expire_time = $scope.storedCredential.expire_time * 1000;
			}

			$scope.getTags = function ($query) {
				return TagService.searchTag($query);
			};

			$scope.currentTab = {
				title: 'General',
				url: 'views/partials/forms/edit_credential/basics.html',
				color: 'blue'
			};

			$scope.onClickTab = function (tab) {
				$scope.currentTab = tab;
			};

			$scope.isActiveTab = function (tab) {
				return tab.url == $scope.currentTab.url;
			};

			/**
			 * Below general edit functions
			 */

			$scope.pwGenerated = function (pass) {
				$scope.storedCredential.password_repeat = pass;
			};

			var _customField = {
				label: '',
				value: '',
				secret: false
			};
			$scope.new_custom_field = angular.copy(_customField);

			$scope.addCustomField = function () {
				if (!$scope.new_custom_field.label) {
					NotificationService.showNotification('Please fill in a label', 3000);
				}
				if (!$scope.new_custom_field.value) {
					NotificationService.showNotification('Please fill in a value!', 3000);
				}
				if (!$scope.new_custom_field.label || !$scope.new_custom_field.value) {
					return;
				}
				$scope.storedCredential.custom_fields.push(angular.copy($scope.new_custom_field));
				$scope.new_custom_field = angular.copy(_customField);
			};

			$scope.deleteCustomField = function (field) {
				var idx = $scope.storedCredential.custom_fields.indexOf(field);
				$scope.storedCredential.custom_fields.splice(idx, 1);
			};

			$scope.new_file = {
				name: '',
				data: null
			};

			$scope.deleteFile = function (file) {
				var idx = $scope.storedCredential.files.indexOf(file);
				FileService.deleteFile(file).then(function () {
					$scope.storedCredential.files.splice(idx, 1);
				});
			};

			$scope.fileLoaded = function (file) {
				var _file = {
					filename: file.name,
					size: file.size,
					mimetype: file.type,
					data: file.data
				};
				FileService.uploadFile(_file).then(function (result) {
					delete result.file_data;
					result.filename = EncryptService.decryptString(result.filename);
					$scope.storedCredential.files.push(result);
				});


				$scope.$digest()
			};

			$scope.fileLoadError = function (error, file) {
				console.log(error, file)
			};

			$scope.selected_file = '';
			$scope.fileprogress = [];
			$scope.fileSelectProgress = function (progress) {
				if (progress) {
					$scope.fileprogress = progress;
					$scope.$digest()

				}
			};
			$scope.renewIntervalValue = 0;
			$scope.renewIntervalModifier = '0';

			$scope.updateInterval = function (renewIntervalValue, renewIntervalModifier) {
				var value = parseInt(renewIntervalValue);
				var modifier = parseInt(renewIntervalModifier);
				if (value && modifier) {<commit_msg>Fix for uploading files to a shared credential<commit_after>'use strict';

/**
 * @ngdoc function
 * @name passmanApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the passmanApp
 */
angular.module('passmanApp')
	.controller('CredentialEditCtrl', ['$scope', 'VaultService', 'CredentialService', 'SettingsService', '$location', '$routeParams', 'FileService', 'EncryptService', 'TagService', 'NotificationService', 'ShareService',
		function ($scope, VaultService, CredentialService, SettingsService, $location, $routeParams, FileService, EncryptService, TagService, NotificationService, ShareService) {
			$scope.active_vault = VaultService.getActiveVault();
			if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
				if (!$scope.active_vault) {
					$location.path('/')
				}
			} else {
				if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
					var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
					VaultService.getVault(_vault).then(function (vault) {
						vault.vaultKey = angular.copy(SettingsService.getSetting('defaultVaultPass'));
						VaultService.setActiveVault(vault);
						$scope.active_vault = vault;

						$scope.pwSettings = VaultService.getVaultSetting('pwSettings',
							{
								'length': 12,
								'useUppercase': true,
								'useLowercase': true,
								'useDigits': true,
								'useSpecialChars': true,
								'minimumDigitCount': 3,
								'avoidAmbiguousCharacters': false,
								'requireEveryCharType': true,
								'generateOnCreate': true
							})
					})
				}
			}

			$scope.tabs = [{
				title: 'General',
				url: 'views/partials/forms/edit_credential/basics.html',
				color: 'blue'
			}, {
				title: 'Password',
				url: 'views/partials/forms/edit_credential/password.html',
				color: 'green'
			}, {
				title: 'Custom fields',
				url: 'views/partials/forms/edit_credential/custom_fields.html',
				color: 'orange'
			}, {
				title: 'Files',
				url: 'views/partials/forms/edit_credential/files.html',
				color: 'yellow'
			}, {
				title: 'OTP',
				url: 'views/partials/forms/edit_credential/otp.html',
				color: 'purple'
			}];


			if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
				if (!$scope.active_vault) {
					$location.path('/')
				}
			} else {
				if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
					var _vault = angular.copy(SettingsService.getSetting('defaultVault'))
					_vault.vaultKey = angular.copy(SettingsService.getSetting('defaultVaultPass'));
					VaultService.setActiveVault(_vault);
					$scope.active_vault = _vault;

				}
			}
			if ($scope.active_vault) {
				$scope.$parent.selectedVault = true;
			}
			var storedCredential = SettingsService.getSetting('edit_credential');

			if (!storedCredential) {
				CredentialService.getCredential($routeParams.credential_id).then(function (result) {
					$scope.storedCredential = CredentialService.decryptCredential(angular.copy(result));
				});
			} else {
				$scope.storedCredential = CredentialService.decryptCredential(angular.copy(storedCredential));
				$scope.storedCredential.password_repeat = angular.copy($scope.storedCredential.password);
				$scope.storedCredential.expire_time = $scope.storedCredential.expire_time * 1000;
			}

			$scope.getTags = function ($query) {
				return TagService.searchTag($query);
			};

			$scope.currentTab = {
				title: 'General',
				url: 'views/partials/forms/edit_credential/basics.html',
				color: 'blue'
			};

			$scope.onClickTab = function (tab) {
				$scope.currentTab = tab;
			};

			$scope.isActiveTab = function (tab) {
				return tab.url == $scope.currentTab.url;
			};

			/**
			 * Below general edit functions
			 */

			$scope.pwGenerated = function (pass) {
				$scope.storedCredential.password_repeat = <PASSWORD>;
			};

			var _customField = {
				label: '',
				value: '',
				secret: false
			};
			$scope.new_custom_field = angular.copy(_customField);

			$scope.addCustomField = function () {
				if (!$scope.new_custom_field.label) {
					NotificationService.showNotification('Please fill in a label', 3000);
				}
				if (!$scope.new_custom_field.value) {
					NotificationService.showNotification('Please fill in a value!', 3000);
				}
				if (!$scope.new_custom_field.label || !$scope.new_custom_field.value) {
					return;
				}
				$scope.storedCredential.custom_fields.push(angular.copy($scope.new_custom_field));
				$scope.new_custom_field = angular.copy(_customField);
			};

			$scope.deleteCustomField = function (field) {
				var idx = $scope.storedCredential.custom_fields.indexOf(field);
				$scope.storedCredential.custom_fields.splice(idx, 1);
			};

			$scope.new_file = {
				name: '',
				data: null
			};

			$scope.deleteFile = function (file) {
				var idx = $scope.storedCredential.files.indexOf(file);
				FileService.deleteFile(file).then(function () {
					$scope.storedCredential.files.splice(idx, 1);
				});
			};

			$scope.fileLoaded = function (file) {
				var key;
				var _file = {
					filename: file.name,
					size: file.size,
					mimetype: file.type,
					data: file.data
				};

				if (!$scope.storedCredential.hasOwnProperty('acl') && $scope.storedCredential.hasOwnProperty('shared_key')) {

					if ($scope.storedCredential.shared_key) {
						key = EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key));
					}
				}

				if ($scope.storedCredential.hasOwnProperty('acl')) {
					key = EncryptService.decryptString(angular.copy($scope.storedCredential.acl.shared_key));
				}


				FileService.uploadFile(_file, key).then(function (result) {
					delete result.file_data;
					result.filename = EncryptService.decryptString(result.filename, key);
					$scope.storedCredential.files.push(result);
				});


				$scope.$digest()
			};

			$scope.fileLoadError = function (error, file) {
				console.log(error, file)
			};

			$scope.selected_file = '';
			$scope.fileprogress = [];
			$scope.fileSelectProgress = function (progress) {
				if (progress) {
					$scope.fileprogress = progress;
					$scope.$digest()

				}
			};
			$scope.renewIntervalValue = 0;
			$scope.renewIntervalModifier = '0';

			$scope.updateInterval = function (renewIntervalValue, renewIntervalModifier) {
				var value = parseInt(renewIntervalValue);
				var modifier = parseInt(renewIntervalModifier);
				if (value && modifier) {
{'fraction_non_alphanumeric': 0.14194932083565018, 'fraction_numerical': 0.003018508221463182, 'mean_word_length': 3.7030257751214046, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3731084', 'n_tokens_mistral': 5051, 'n_tokens_neox': 3939, 'n_words': 602}