Ejemplo n.º 1
0
		public HttpResponseMessage Patch(string name, string rename)
		{
			name = RavenFileNameHelper.RavenPath(name);
			rename = RavenFileNameHelper.RavenPath(rename);

			try
			{
				ConcurrencyAwareExecutor.Execute(() =>
												 Storage.Batch(accessor =>
												 {
													 AssertFileIsNotBeingSynced(name, accessor, true);

													 var metadata = accessor.GetFile(name, 0, 0).Metadata;

													 if (
														 metadata.AllKeys.Contains(
															 SynchronizationConstants.RavenDeleteMarker))
													 {
														 throw new FileNotFoundException();
													 }

													 var existingHeader = accessor.ReadFile(rename);
													 if (existingHeader != null &&
														 !existingHeader.Metadata.AllKeys.Contains(
															 SynchronizationConstants.RavenDeleteMarker))
													 {
														 throw new HttpResponseException(
															 Request.CreateResponse(HttpStatusCode.Forbidden,
																					new InvalidOperationException(
																						"Cannot rename because file " + rename +
																						" already exists")));
													 }

													 Historian.UpdateLastModified(metadata);

													 var operation = new RenameFileOperation
													 {
														 Name = name,
														 Rename = rename,
														 MetadataAfterOperation = metadata
													 };

													 accessor.SetConfig(
														 RavenFileNameHelper.RenameOperationConfigNameForFile(name),
														 operation.AsConfig());
													 accessor.PulseTransaction(); // commit rename operation config

													 StorageOperationsTask.RenameFile(operation);
												 }), ConcurrencyResponseException);
			}
			catch (FileNotFoundException)
			{
				log.Debug("Cannot rename a file '{0}' to '{1}' because a file was not found", name, rename);
				return new HttpResponseMessage(HttpStatusCode.NotFound);
			}

			log.Debug("File '{0}' was renamed to '{1}'", name, rename);

			StartSynchronizeDestinationsInBackground();

			return new HttpResponseMessage(HttpStatusCode.NoContent);
		}