/// <summary> /// Updates the segment metadata status. /// </summary> /// <param name="metadata">The metadata.</param> /// <param name="segmentNumber">The segment number.</param> /// <param name="newStatus">The new status.</param> private static void UpdateSegmentMetadataStatus(UploadMetadata metadata, int segmentNumber, SegmentUploadStatus newStatus) { metadata.Segments[segmentNumber].Status = newStatus; try { metadata.Save(); } catch { } //no need to crash the program if were unable to save the metadata; it is what's in memory that's important }
/// <summary> /// Creates a new metadata based on the given input parameters, and saves it to its canonical location. /// </summary> /// <param name="metadataFilePath">The metadata file path.</param> /// <returns></returns> public UploadMetadata CreateNewMetadata(string metadataFilePath) { //create metadata var metadata = new UploadMetadata(metadataFilePath, _parameters, _frontend); //save the initial version metadata.Save(); return(metadata); }
/// <summary> /// Creates a new metadata based on the given input parameters, and saves it to its canonical location. /// </summary> /// <returns></returns> public UploadMetadata CreateNewMetadata(string metadataFilePath) { //determine segment count, segment length and Upload Id //create metadata var metadata = new UploadMetadata(metadataFilePath, _parameters); if (!_parameters.IsBinary && metadata.SegmentCount > 1) { this.AlignSegmentsToRecordBoundaries(metadata); } //save the initial version metadata.Save(); return(metadata); }
/// <summary> /// Validates that the metadata is valid for a resume operation, and also updates the internal Segment States to match what the Server looks like. /// If any changes are made, the metadata will be saved to its canonical location. /// </summary> /// <param name="metadata"></param> private void ValidateMetadataForResume(UploadMetadata metadata) { ValidateMetadataMatchesLocalFile(metadata); //verify that the target stream does not already exist (in case we don't want to overwrite) if (!this.Parameters.IsOverwrite && _frontEnd.StreamExists(metadata.TargetStreamPath)) { throw new InvalidOperationException("Target Stream already exists"); } //make sure we don't upload part of the file as binary, while the rest is non-binary (that's just asking for trouble) if (this.Parameters.IsBinary != metadata.IsBinary) { throw new InvalidOperationException( string.Format( "Existing metadata was created for a {0}binary file while the current parameters requested a {1}binary upload.", metadata.IsBinary ? string.Empty : "non-", this.Parameters.IsBinary ? string.Empty : "non-")); } //see what files(segments) already exist - update metadata accordingly (only for segments that are missing from server; if it's on the server but not in metadata, reupload) foreach (var segment in metadata.Segments) { if (segment.Status == SegmentUploadStatus.Complete) { var retryCount = 0; while (retryCount < SingleSegmentUploader.MaxBufferUploadAttemptCount) { _token.ThrowIfCancellationRequested(); retryCount++; try { //verify that the stream exists and that the length is as expected if (!_frontEnd.StreamExists(segment.Path)) { // this segment was marked as completed, but no target stream exists; it needs to be reuploaded segment.Status = SegmentUploadStatus.Pending; } else { var remoteLength = _frontEnd.GetStreamLength(segment.Path); if (remoteLength != segment.Length) { //the target stream has a different length than the input segment, which implies they are inconsistent; it needs to be reuploaded segment.Status = SegmentUploadStatus.Pending; } } break; } catch (Exception e) { _token.ThrowIfCancellationRequested(); if (retryCount >= SingleSegmentUploader.MaxBufferUploadAttemptCount) { throw new UploadFailedException( string.Format( "Cannot validate metadata in order to resume due to the following exception retrieving file information: {0}", e)); } SingleSegmentUploader.WaitForRetry(retryCount, Parameters.UseSegmentBlockBackOffRetryStrategy, _token); } } } else { //anything which is not in 'Completed' status needs to be reuploaded segment.Status = SegmentUploadStatus.Pending; } } metadata.Save(); }
/// <summary> /// Creates a new metadata based on the given input parameters, and saves it to its canonical location. /// </summary> /// <returns></returns> public UploadMetadata CreateNewMetadata(string metadataFilePath) { //determine segment count, segment length and Upload Id //create metadata var metadata = new UploadMetadata(metadataFilePath, _parameters); if (!_parameters.IsBinary && metadata.SegmentCount > 1) { this.AlignSegmentsToRecordBoundaries(metadata); } //save the initial version metadata.Save(); return metadata; }
/// <summary> /// Updates the segment metadata status. /// </summary> /// <param name="metadata">The metadata.</param> /// <param name="segmentNumber">The segment number.</param> /// <param name="newStatus">The new status.</param> private static void UpdateSegmentMetadataStatus(UploadMetadata metadata, int segmentNumber, SegmentUploadStatus newStatus) { metadata.Segments[segmentNumber].Status = newStatus; try { metadata.Save(); } catch { } //no need to crash the program if were unable to save the metadata; it is what's in memory that's important }
/// <summary> /// Validates that the metadata is valid for a resume operation, and also updates the internal Segment States to match what the Server looks like. /// If any changes are made, the metadata will be saved to its canonical location. /// </summary> /// <param name="metadata"></param> private void ValidateMetadataForResume(UploadMetadata metadata) { ValidateMetadataMatchesLocalFile(metadata); //verify that the target stream does not already exist (in case we don't want to overwrite) if (!this.Parameters.IsOverwrite && _frontEnd.StreamExists(metadata.TargetStreamPath)) { throw new InvalidOperationException("Target Stream already exists"); } //make sure we don't upload part of the file as binary, while the rest is non-binary (that's just asking for trouble) if (this.Parameters.IsBinary != metadata.IsBinary) { throw new InvalidOperationException( string.Format( "Existing metadata was created for a {0}binary file while the current parameters requested a {1}binary upload.", metadata.IsBinary ? string.Empty : "non-", this.Parameters.IsBinary ? string.Empty : "non-")); } //see what files(segments) already exist - update metadata accordingly (only for segments that are missing from server; if it's on the server but not in metadata, reupload) foreach (var segment in metadata.Segments) { if (segment.Status == SegmentUploadStatus.Complete) { var retryCount = 0; while (retryCount < SingleSegmentUploader.MaxBufferUploadAttemptCount) { _token.ThrowIfCancellationRequested(); retryCount++; try { //verify that the stream exists and that the length is as expected if (!_frontEnd.StreamExists(segment.Path)) { // this segment was marked as completed, but no target stream exists; it needs to be reuploaded segment.Status = SegmentUploadStatus.Pending; } else { var remoteLength = _frontEnd.GetStreamLength(segment.Path); if (remoteLength != segment.Length) { //the target stream has a different length than the input segment, which implies they are inconsistent; it needs to be reuploaded segment.Status = SegmentUploadStatus.Pending; } } break; } catch (Exception e) { _token.ThrowIfCancellationRequested(); if (retryCount >= SingleSegmentUploader.MaxBufferUploadAttemptCount) { throw new UploadFailedException( string.Format( "Cannot validate metadata in order to resume due to the following exception retrieving file information: {0}", e)); } SingleSegmentUploader.WaitForRetry(retryCount, Parameters.UseSegmentBlockBackOffRetryStrategy, _token); } } } else { //anything which is not in 'Completed' status needs to be reuploaded segment.Status = SegmentUploadStatus.Pending; } } metadata.Save(); }