public async Task<HttpResponseMessage> Put(string name, string uploadId = null)
		{
			try
			{
                RavenFileSystem.MetricsCounters.FilesPerSecond.Mark();

				name = RavenFileNameHelper.RavenPath(name);

                var headers = this.GetFilteredMetadataFromHeaders(InnerHeaders);

                Historian.UpdateLastModified(headers);
                Historian.Update(name, headers);

                SynchronizationTask.Cancel(name);

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

                    var contentLength = Request.Content.Headers.ContentLength;
                    var sizeHeader = GetHeader("RavenFS-size");
                    long? size;
                    long sizeForParse;
                    if (contentLength == 0 || long.TryParse(sizeHeader, out sizeForParse) == false)
                    {
                        size = contentLength;
                        if (Request.Headers.TransferEncodingChunked ?? false)
                        {
                            size = null;
                        }
                    }
                    else
                    {
                        size = sizeForParse;
                    }

                    accessor.PutFile(name, size, headers);
                    Search.Index(name, headers);                  
                }));

                log.Debug("Inserted a new file '{0}' with ETag {1}", name, headers.Value<Guid>("ETag"));

                using (var contentStream = await Request.Content.ReadAsStreamAsync())
                using (var readFileToDatabase = new ReadFileToDatabase(BufferPool, Storage, contentStream, name))
                {
                    await readFileToDatabase.Execute();

                    Historian.UpdateLastModified(headers); // update with the final file size

                    log.Debug("File '{0}' was uploaded. Starting to update file metadata and indexes", name);

                    headers["Content-MD5"] = readFileToDatabase.FileHash;

                    Storage.Batch(accessor => accessor.UpdateFileMetadata(name, headers));
                    headers["Content-Length"] = readFileToDatabase.TotalSizeRead.ToString(CultureInfo.InvariantCulture);
                    Search.Index(name, headers);
                    Publisher.Publish(new FileChangeNotification { Action = FileChangeAction.Add, File = FilePathTools.Cannoicalise(name) });

                    log.Debug("Updates of '{0}' metadata and indexes were finished. New file ETag is {1}", name, headers.Value<Guid>("ETag"));

                    StartSynchronizeDestinationsInBackground();
                }
			}
			catch (Exception ex)
			{
				if (uploadId != null)
				{
					Guid uploadIdentifier;
					if (Guid.TryParse(uploadId, out uploadIdentifier))
					{
						Publisher.Publish(new CancellationNotification { UploadId = uploadIdentifier, File = name });
					}
				}

				log.WarnException(string.Format("Failed to upload a file '{0}'", name), ex);

				var concurrencyException = ex as ConcurrencyException;
				if (concurrencyException != null)
				{
					throw ConcurrencyResponseException(concurrencyException);
				}

				throw;
			}

            return GetEmptyMessage(HttpStatusCode.Created);
		}
		public async Task<HttpResponseMessage> Put(string name, string uploadId = null, bool preserveTimestamps = false)
		{                     
			try
			{
                FileSystem.MetricsCounters.FilesPerSecond.Mark();

                name = FileHeader.Canonize(name);

                var headers = this.GetFilteredMetadataFromHeaders(ReadInnerHeaders);
                if (preserveTimestamps)
                {
                    if (!headers.ContainsKey(Constants.RavenCreationDate))
                    {
                        if (headers.ContainsKey(Constants.CreationDate))
                            headers[Constants.RavenCreationDate] = headers[Constants.CreationDate];                            
                        else
                            throw new InvalidOperationException("Preserve Timestamps requires that the client includes the Raven-Creation-Date header.");
                    }

                    var lastModified = GetHeader(Constants.RavenLastModified);
                    if ( lastModified != null)
                    {
                        DateTimeOffset when;
                        if (!DateTimeOffset.TryParse(lastModified, out when))
                            when = DateTimeOffset.UtcNow;

                        Historian.UpdateLastModified(headers, when);
                    }
                    else Historian.UpdateLastModified(headers);
                }
                else
                {
                    headers[Constants.RavenCreationDate] = DateTimeOffset.UtcNow;

                    Historian.UpdateLastModified(headers);
                }

                // TODO: To keep current filesystems working. We should remove when adding a new migration. 
                headers[Constants.CreationDate] = headers[Constants.RavenCreationDate].Value<DateTimeOffset>().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ", CultureInfo.InvariantCulture);

                Historian.Update(name, headers);

                SynchronizationTask.Cancel(name);

                long? size = -1;
                ConcurrencyAwareExecutor.Execute(() => Storage.Batch(accessor =>
                {
					AssertPutOperationNotVetoed(name, headers);
                    AssertFileIsNotBeingSynced(name, accessor, true);

                    var contentLength = Request.Content.Headers.ContentLength;
                    var sizeHeader = GetHeader("RavenFS-size");

                    long sizeForParse;
                    if (contentLength == 0 || long.TryParse(sizeHeader, out sizeForParse) == false)
                    {
                        size = contentLength;
                        if (Request.Headers.TransferEncodingChunked ?? false)
                        {
                            size = null;
                        }
                    }
                    else
                    {
                        size = sizeForParse;
                    }

					FileSystem.PutTriggers.Apply(trigger => trigger.OnPut(name, headers));

	                using (FileSystem.DisableAllTriggersForCurrentThread())
	                {
						StorageOperationsTask.IndicateFileToDelete(name);
	                }

                    accessor.PutFile(name, size, headers);

					FileSystem.PutTriggers.Apply(trigger => trigger.AfterPut(name, size, headers));

                    Search.Index(name, headers);                  
                }));

                log.Debug("Inserted a new file '{0}' with ETag {1}", name, headers.Value<Guid>(Constants.MetadataEtagField));

                using (var contentStream = await Request.Content.ReadAsStreamAsync())
                using (var readFileToDatabase = new ReadFileToDatabase(BufferPool, Storage, FileSystem.PutTriggers, contentStream, name, headers))
                {
                    await readFileToDatabase.Execute();                
   
                    if (readFileToDatabase.TotalSizeRead != size)
                    {
                        StorageOperationsTask.IndicateFileToDelete(name);
                        throw new HttpResponseException(HttpStatusCode.BadRequest);
                    }                        

                    if ( !preserveTimestamps )
                        Historian.UpdateLastModified(headers); // update with the final file size.

                    log.Debug("File '{0}' was uploaded. Starting to update file metadata and indexes", name);

                    headers["Content-MD5"] = readFileToDatabase.FileHash;                    

                    Storage.Batch(accessor => accessor.UpdateFileMetadata(name, headers));

                    int totalSizeRead = readFileToDatabase.TotalSizeRead;
                    headers["Content-Length"] = totalSizeRead.ToString(CultureInfo.InvariantCulture);
                    
                    Search.Index(name, headers);
                    Publisher.Publish(new FileChangeNotification { Action = FileChangeAction.Add, File = FilePathTools.Cannoicalise(name) });

                    log.Debug("Updates of '{0}' metadata and indexes were finished. New file ETag is {1}", name, headers.Value<Guid>(Constants.MetadataEtagField));

                    StartSynchronizeDestinationsInBackground();
                }
			}
			catch (Exception ex)
			{
				if (uploadId != null)
				{
					Guid uploadIdentifier;
					if (Guid.TryParse(uploadId, out uploadIdentifier))
					{
						Publisher.Publish(new CancellationNotification { UploadId = uploadIdentifier, File = name });
					}
				}

				log.WarnException(string.Format("Failed to upload a file '{0}'", name), ex);

				var concurrencyException = ex as ConcurrencyException;
				if (concurrencyException != null)
				{
					throw ConcurrencyResponseException(concurrencyException);
				}

				throw;
			}

			return GetEmptyMessage(HttpStatusCode.Created);
		}
Beispiel #3
0
        public async Task <HttpResponseMessage> Put(string name, string uploadId = null)
        {
            try
            {
                FileSystem.MetricsCounters.FilesPerSecond.Mark();

                name = RavenFileNameHelper.RavenPath(name);

                var headers = this.GetFilteredMetadataFromHeaders(InnerHeaders);

                Historian.UpdateLastModified(headers);
                headers["Creation-Date"] = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture);
                Historian.Update(name, headers);

                SynchronizationTask.Cancel(name);

                long?size = -1;
                ConcurrencyAwareExecutor.Execute(() => Storage.Batch(accessor =>
                {
                    AssertFileIsNotBeingSynced(name, accessor, true);
                    StorageOperationsTask.IndicateFileToDelete(name);

                    var contentLength = Request.Content.Headers.ContentLength;
                    var sizeHeader    = GetHeader("RavenFS-size");

                    long sizeForParse;
                    if (contentLength == 0 || long.TryParse(sizeHeader, out sizeForParse) == false)
                    {
                        size = contentLength;
                        if (Request.Headers.TransferEncodingChunked ?? false)
                        {
                            size = null;
                        }
                    }
                    else
                    {
                        size = sizeForParse;
                    }

                    accessor.PutFile(name, size, headers);

                    Search.Index(name, headers);
                }));

                log.Debug("Inserted a new file '{0}' with ETag {1}", name, headers.Value <Guid>(Constants.MetadataEtagField));

                using (var contentStream = await Request.Content.ReadAsStreamAsync())
                    using (var readFileToDatabase = new ReadFileToDatabase(BufferPool, Storage, contentStream, name))
                    {
                        await readFileToDatabase.Execute();

                        if (readFileToDatabase.TotalSizeRead != size)
                        {
                            Storage.Batch(accessor => { StorageOperationsTask.IndicateFileToDelete(name); });
                            throw new HttpResponseException(HttpStatusCode.BadRequest);
                        }

                        Historian.UpdateLastModified(headers); // update with the final file size

                        log.Debug("File '{0}' was uploaded. Starting to update file metadata and indexes", name);

                        headers["Content-MD5"] = readFileToDatabase.FileHash;

                        Storage.Batch(accessor => accessor.UpdateFileMetadata(name, headers));

                        int totalSizeRead = readFileToDatabase.TotalSizeRead;
                        headers["Content-Length"] = totalSizeRead.ToString(CultureInfo.InvariantCulture);

                        Search.Index(name, headers);
                        Publisher.Publish(new FileChangeNotification {
                            Action = FileChangeAction.Add, File = FilePathTools.Cannoicalise(name)
                        });

                        log.Debug("Updates of '{0}' metadata and indexes were finished. New file ETag is {1}", name, headers.Value <Guid>(Constants.MetadataEtagField));

                        StartSynchronizeDestinationsInBackground();
                    }
            }
            catch (Exception ex)
            {
                if (uploadId != null)
                {
                    Guid uploadIdentifier;
                    if (Guid.TryParse(uploadId, out uploadIdentifier))
                    {
                        Publisher.Publish(new CancellationNotification {
                            UploadId = uploadIdentifier, File = name
                        });
                    }
                }

                log.WarnException(string.Format("Failed to upload a file '{0}'", name), ex);

                var concurrencyException = ex as ConcurrencyException;
                if (concurrencyException != null)
                {
                    throw ConcurrencyResponseException(concurrencyException);
                }

                throw;
            }

            return(GetEmptyMessage(HttpStatusCode.Created));
        }
Beispiel #4
0
        public async Task <HttpResponseMessage> Put(string name, string uploadId = null, bool preserveTimestamps = false)
        {
            try
            {
                FileSystem.MetricsCounters.FilesPerSecond.Mark();

                name = FileHeader.Canonize(name);

                var headers = this.GetFilteredMetadataFromHeaders(ReadInnerHeaders);
                if (preserveTimestamps)
                {
                    if (!headers.ContainsKey(Constants.RavenCreationDate))
                    {
                        if (headers.ContainsKey(Constants.CreationDate))
                        {
                            headers[Constants.RavenCreationDate] = headers[Constants.CreationDate];
                        }
                        else
                        {
                            throw new InvalidOperationException("Preserve Timestamps requires that the client includes the Raven-Creation-Date header.");
                        }
                    }

                    var lastModified = GetHeader(Constants.RavenLastModified);
                    if (lastModified != null)
                    {
                        DateTimeOffset when;
                        if (!DateTimeOffset.TryParse(lastModified, out when))
                        {
                            when = DateTimeOffset.UtcNow;
                        }

                        Historian.UpdateLastModified(headers, when);
                    }
                    else
                    {
                        Historian.UpdateLastModified(headers);
                    }
                }
                else
                {
                    headers[Constants.RavenCreationDate] = DateTimeOffset.UtcNow;

                    Historian.UpdateLastModified(headers);
                }

                // TODO: To keep current filesystems working. We should remove when adding a new migration.
                headers[Constants.CreationDate] = headers[Constants.RavenCreationDate].Value <DateTimeOffset>().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ", CultureInfo.InvariantCulture);

                Historian.Update(name, headers);

                SynchronizationTask.Cancel(name);

                long?size = -1;
                ConcurrencyAwareExecutor.Execute(() => Storage.Batch(accessor =>
                {
                    AssertPutOperationNotVetoed(name, headers);
                    AssertFileIsNotBeingSynced(name, accessor, true);

                    var contentLength = Request.Content.Headers.ContentLength;
                    var sizeHeader    = GetHeader("RavenFS-size");

                    long sizeForParse;
                    if (contentLength == 0 || long.TryParse(sizeHeader, out sizeForParse) == false)
                    {
                        size = contentLength;
                        if (Request.Headers.TransferEncodingChunked ?? false)
                        {
                            size = null;
                        }
                    }
                    else
                    {
                        size = sizeForParse;
                    }

                    FileSystem.PutTriggers.Apply(trigger => trigger.OnPut(name, headers));

                    using (FileSystem.DisableAllTriggersForCurrentThread())
                    {
                        StorageOperationsTask.IndicateFileToDelete(name);
                    }

                    accessor.PutFile(name, size, headers);

                    FileSystem.PutTriggers.Apply(trigger => trigger.AfterPut(name, size, headers));

                    Search.Index(name, headers);
                }));

                log.Debug("Inserted a new file '{0}' with ETag {1}", name, headers.Value <Guid>(Constants.MetadataEtagField));

                using (var contentStream = await Request.Content.ReadAsStreamAsync())
                    using (var readFileToDatabase = new ReadFileToDatabase(BufferPool, Storage, FileSystem.PutTriggers, contentStream, name, headers))
                    {
                        await readFileToDatabase.Execute();

                        if (readFileToDatabase.TotalSizeRead != size)
                        {
                            StorageOperationsTask.IndicateFileToDelete(name);
                            throw new HttpResponseException(HttpStatusCode.BadRequest);
                        }

                        if (!preserveTimestamps)
                        {
                            Historian.UpdateLastModified(headers); // update with the final file size.
                        }
                        log.Debug("File '{0}' was uploaded. Starting to update file metadata and indexes", name);

                        headers["Content-MD5"] = readFileToDatabase.FileHash;

                        Storage.Batch(accessor => accessor.UpdateFileMetadata(name, headers));

                        int totalSizeRead = readFileToDatabase.TotalSizeRead;
                        headers["Content-Length"] = totalSizeRead.ToString(CultureInfo.InvariantCulture);

                        Search.Index(name, headers);
                        Publisher.Publish(new FileChangeNotification {
                            Action = FileChangeAction.Add, File = FilePathTools.Cannoicalise(name)
                        });

                        log.Debug("Updates of '{0}' metadata and indexes were finished. New file ETag is {1}", name, headers.Value <Guid>(Constants.MetadataEtagField));

                        StartSynchronizeDestinationsInBackground();
                    }
            }
            catch (Exception ex)
            {
                if (uploadId != null)
                {
                    Guid uploadIdentifier;
                    if (Guid.TryParse(uploadId, out uploadIdentifier))
                    {
                        Publisher.Publish(new CancellationNotification {
                            UploadId = uploadIdentifier, File = name
                        });
                    }
                }

                log.WarnException(string.Format("Failed to upload a file '{0}'", name), ex);

                var concurrencyException = ex as ConcurrencyException;
                if (concurrencyException != null)
                {
                    throw ConcurrencyResponseException(concurrencyException);
                }

                throw;
            }

            return(GetEmptyMessage(HttpStatusCode.Created));
        }