Ejemplo n.º 1
0
        // Called by AzureBlobSyncProvider.UpdateItem to help with writing item updates
        internal SyncedBlobAttributes UpdateFile(
            string oldName,
            FileData fileData,
            string relativePath,
            Stream dataStream,
            DateTime expectedLastModified
            )
        {
            CloudBlob blob = Container.GetBlobReference(oldName);

            try
            {
                blob.FetchAttributes();
            }
            catch (StorageClientException e)
            {
                // Someone may have deleted the blob in the mean time
                if (e.ErrorCode == StorageErrorCode.BlobNotFound)
                {
                    throw new ApplicationException("Concurrency Violation", e);
                }
                throw;
            }
            BlobProperties blobProperties = blob.Properties;

            //
            // For directories create an empty data stream
            //
            if (dataStream == null)
            {
                dataStream = new MemoryStream();
            }

            SetupMetadata(blob.Metadata, fileData, relativePath);
            blobProperties.ContentType = LookupMimeType(Path.GetExtension(fileData.Name));

            // Specify an optimistic concurrency check to prevent races with other endpoints syncing at the same time.
            BlobRequestOptions opts = new BlobRequestOptions();

            opts.AccessCondition = AccessCondition.IfNotModifiedSince(expectedLastModified);

            try
            {
                blob.UploadFromStream(dataStream, opts);
            }
            catch (StorageClientException e)
            {
                // Someone must have modified the file in the mean time
                if (e.ErrorCode == StorageErrorCode.BlobNotFound || e.ErrorCode == StorageErrorCode.ConditionFailed)
                {
                    throw new ApplicationException("Storage Error", e);
                }
                throw;
            }

            blobProperties = blob.Properties;
            SyncedBlobAttributes attributes = new SyncedBlobAttributes(blob.Uri.ToString(), blobProperties.LastModifiedUtc);

            return(attributes);
        }
Ejemplo n.º 2
0
        // Called by AzureBlobSyncProvider.InsertItem.
        internal SyncedBlobAttributes InsertFile(FileData fileData, string relativePath, Stream dataStream)
        {
            if (fileData.Name.Length > MaxFileNameLength)
            {
                throw new ApplicationException("Name Too Long");
            }

            CloudBlob      blob           = Container.GetBlobReference(fileData.RelativePath.ToLower());
            BlobProperties blobProperties = blob.Properties;
            DateTime       uninitTime     = blobProperties.LastModifiedUtc;

            SetupMetadata(blob.Metadata, fileData, relativePath);
            blobProperties.ContentType = LookupMimeType(Path.GetExtension(fileData.Name));

            if (fileData.IsDirectory)
            {
                // Directories have no stream
                dataStream = new MemoryStream();
            }

            // Specify an optimistic concurrency check to prevent races with other endpoints syncing at the same time.
            BlobRequestOptions opts = new BlobRequestOptions();

            opts.AccessCondition = AccessCondition.IfNotModifiedSince(uninitTime);

            try
            {
                blob.UploadFromStream(dataStream, opts);
            }
            catch (StorageException e)
            {
                if (e.ErrorCode == StorageErrorCode.BlobAlreadyExists || e.ErrorCode == StorageErrorCode.ConditionFailed)
                {
                    throw new ApplicationException("Concurrency Violation", e);
                }
                throw;
            }

            blobProperties = blob.Properties;
            SyncedBlobAttributes attributes = new SyncedBlobAttributes(blob.Uri.ToString(), blobProperties.LastModifiedUtc);

            return(attributes);
        }
Ejemplo n.º 3
0
        // Called by the framework when an item (file) needs to be added to the store.
        public override void InsertItem(
            object itemData,
            IEnumerable <SyncId> changeUnitsToCreate,
            RecoverableErrorReportingContext recoverableErrorReportingContext,
            out ItemFieldDictionary keyAndUpdatedVersion,
            out bool commitKnowledgeAfterThisItem
            )
        {
            IFileDataRetriever dataRetriever = (IFileDataRetriever)itemData;

            string relativePath = dataRetriever.RelativeDirectoryPath;

            keyAndUpdatedVersion         = null;
            commitKnowledgeAfterThisItem = false;

            ApplyingBlobEventArgs args = new ApplyingBlobEventArgs(ChangeType.Create, dataRetriever.FileData.Name);
            EventHandler <ApplyingBlobEventArgs> applyingDelegate = ApplyingChange;

            applyingDelegate(this, args);

            try
            {
                Stream dataStream = null;
                if (!dataRetriever.FileData.IsDirectory)
                {
                    dataStream = dataRetriever.FileStream;
                }

                SyncedBlobAttributes attrs = DataStore.InsertFile(dataRetriever.FileData, dataRetriever.RelativeDirectoryPath, dataStream);
                keyAndUpdatedVersion = new ItemFieldDictionary();
                keyAndUpdatedVersion.Add(new ItemField(ItemFields.CUSTOM_FIELD_NAME, typeof(string), attrs._name));
                keyAndUpdatedVersion.Add(new ItemField(ItemFields.CUSTOM_FIELD_TIMESTAMP, typeof(ulong), (ulong)attrs._lastModifiedTime.ToBinary()));
            }
            catch (ApplicationException e)
            {
                recoverableErrorReportingContext.RecordRecoverableErrorForChange(new RecoverableErrorData(e));
            }
        }