| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.IO; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Text; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using Models = Microsoft.Azure.Batch.Protocol.Models; |
| | 11 | | using System.Threading; |
| | 12 | |
|
| | 13 | | namespace Microsoft.Azure.Batch |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Exposes methods and properties to access files from Nodes or Tasks. |
| | 17 | | /// </summary> |
| | 18 | | public abstract class NodeFile : IInheritedBehaviors |
| | 19 | | { |
| | 20 | | internal FileItemBox fileItemBox; |
| | 21 | |
|
| | 22 | | #region // constructors |
| | 23 | |
|
| 0 | 24 | | private NodeFile() |
| | 25 | | { |
| 0 | 26 | | } |
| | 27 | |
|
| 8 | 28 | | internal NodeFile(Models.NodeFile boundToThis, IEnumerable<BatchClientBehavior> inheritTheseBehaviors) |
| | 29 | | { |
| 8 | 30 | | this.fileItemBox = new FileItemBox(boundToThis); |
| | 31 | |
|
| | 32 | | // inherit from parent |
| 8 | 33 | | InheritUtil.InheritClientBehaviorsAndSetPublicProperty(this, inheritTheseBehaviors); |
| 8 | 34 | | } |
| | 35 | |
|
| | 36 | | #endregion |
| | 37 | |
|
| | 38 | | #region IInheritedBehaviors |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Gets or sets a list of behaviors that modify or customize requests to the Batch service |
| | 42 | | /// made via this <see cref="NodeFile"/>. |
| | 43 | | /// </summary> |
| | 44 | | /// <remarks> |
| | 45 | | /// <para>These behaviors are inherited by child objects.</para> |
| | 46 | | /// <para>Modifications are applied in the order of the collection. The last write wins.</para> |
| | 47 | | /// </remarks> |
| 14 | 48 | | public IList<BatchClientBehavior> CustomBehaviors { get; set; } |
| | 49 | |
|
| | 50 | | #endregion IInheritedBehaviors |
| | 51 | |
|
| | 52 | | #region // NodeFile |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Gets the value that indicates whether the file is a directory. |
| | 56 | | /// </summary> |
| | 57 | | public bool? IsDirectory |
| | 58 | | { |
| | 59 | | get |
| | 60 | | { |
| 0 | 61 | | return this.fileItemBox.IsDirectory; |
| | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Gets the name of the file. |
| | 67 | | /// </summary> |
| | 68 | | [Obsolete("Obsolete as of 02/2017. Use Path instead.")] |
| | 69 | | public string Name |
| | 70 | | { |
| | 71 | | get |
| | 72 | | { |
| 0 | 73 | | return this.fileItemBox.Name; |
| | 74 | | } |
| | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Gets the path of the file. |
| | 79 | | /// </summary> |
| | 80 | | public string Path |
| | 81 | | { |
| | 82 | | get |
| | 83 | | { |
| 6 | 84 | | return this.fileItemBox.Name; |
| | 85 | | } |
| | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Gets the FileProperties of the file. |
| | 90 | | /// </summary> |
| | 91 | | public FileProperties Properties |
| | 92 | | { |
| | 93 | | get |
| | 94 | | { |
| 2 | 95 | | return this.fileItemBox.Properties; |
| | 96 | | } |
| | 97 | | } |
| | 98 | |
|
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Gets the URL of the file. |
| | 102 | | /// </summary> |
| | 103 | | public string Url |
| | 104 | | { |
| | 105 | | get |
| | 106 | | { |
| 0 | 107 | | return this.fileItemBox.Url; |
| | 108 | | } |
| | 109 | | } |
| | 110 | |
|
| | 111 | | /// <summary> |
| | 112 | | /// Begins asynchronous call to return the contents of the file as a string. |
| | 113 | | /// </summary> |
| | 114 | | /// <param name="encoding">The encoding used to interpret the file data. If no value or null is specified, UTF8 |
| | 115 | | /// <param name="byteRange">The file byte range to retrieve. If null, the entire file is retrieved.</param> |
| | 116 | | /// <param name="additionalBehaviors">A collection of BatchClientBehavior instances that are applied after the C |
| | 117 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 118 | | /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</ret |
| | 119 | | public Task<string> ReadAsStringAsync( |
| | 120 | | Encoding encoding = null, |
| | 121 | | GetFileRequestByteRange byteRange = null, |
| | 122 | | IEnumerable<BatchClientBehavior> additionalBehaviors = null, |
| | 123 | | CancellationToken cancellationToken = default(CancellationToken)) |
| | 124 | | { |
| 2 | 125 | | return UtilitiesInternal.ReadNodeFileAsStringAsync( |
| 2 | 126 | | CopyToStreamAsync, |
| 2 | 127 | | encoding, |
| 2 | 128 | | byteRange, |
| 2 | 129 | | additionalBehaviors, |
| 2 | 130 | | cancellationToken); |
| | 131 | | } |
| | 132 | |
|
| | 133 | | /// <summary> |
| | 134 | | /// Blocking call to return the contents of the file as a string. |
| | 135 | | /// </summary> |
| | 136 | | /// <param name="encoding">The encoding used to interpret the file data. If no value or null is specified, UTF8 |
| | 137 | | /// <param name="byteRange">The file byte range to retrieve. If null, the entire file is retrieved.</param> |
| | 138 | | /// <param name="additionalBehaviors">A collection of BatchClientBehavior instances that are applied after the C |
| | 139 | | /// <returns>A string containing the contents of the file.</returns> |
| | 140 | | public string ReadAsString(Encoding encoding = null, GetFileRequestByteRange byteRange = null, IEnumerable<Batch |
| | 141 | | { |
| 0 | 142 | | Task<string> asyncTask = ReadAsStringAsync(encoding, byteRange, additionalBehaviors); |
| 0 | 143 | | string readAsString = asyncTask.WaitAndUnaggregateException(this.CustomBehaviors, additionalBehaviors); |
| | 144 | |
|
| | 145 | | // return result |
| 0 | 146 | | return readAsString; |
| | 147 | | } |
| | 148 | |
|
| | 149 | | /// <summary> |
| | 150 | | /// Begins an asynchronous call to copy the contents of the file into the given Stream. |
| | 151 | | /// </summary> |
| | 152 | | /// <param name="stream">The stream into which the contents of the file are copied.</param> |
| | 153 | | /// <param name="byteRange">The file byte range to retrieve. If null, the entire file is retrieved.</param> |
| | 154 | | /// <param name="additionalBehaviors">A collection of BatchClientBehavior instances that are applied after the C |
| | 155 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 156 | | /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</ret |
| | 157 | | public abstract Task CopyToStreamAsync(Stream stream, |
| | 158 | | GetFileRequestByteRange byteRange = null, |
| | 159 | | IEnumerable<BatchClientBehavior> additionalBehaviors = null, |
| | 160 | | CancellationToken cancellationToken = default(CancellationToken)); |
| | 161 | |
|
| | 162 | | /// <summary> |
| | 163 | | /// Blocking call to copy the contents of the file into the given Stream. |
| | 164 | | /// </summary> |
| | 165 | | /// <param name="stream">The stream into which the contents of the file are copied.</param> |
| | 166 | | /// <param name="byteRange">The file byte range to retrieve. If null, the entire file is retrieved.</param> |
| | 167 | | /// <param name="additionalBehaviors">A collection of BatchClientBehavior instances that are applied after the C |
| | 168 | | public virtual void CopyToStream(Stream stream, GetFileRequestByteRange byteRange = null, IEnumerable<BatchClien |
| | 169 | | { |
| 0 | 170 | | Task asyncTask = CopyToStreamAsync(stream, byteRange, additionalBehaviors); |
| 0 | 171 | | asyncTask.WaitAndUnaggregateException(this.CustomBehaviors, additionalBehaviors); |
| 0 | 172 | | } |
| | 173 | |
|
| | 174 | | /// <summary> |
| | 175 | | /// Begins an asynchronous call to delete the file. |
| | 176 | | /// </summary> |
| | 177 | | /// <param name="recursive"> |
| | 178 | | /// If the file-path parameter represents a directory instead of a file, you can set the optional |
| | 179 | | /// recursive parameter to true to delete the directory and all of the files and subdirectories in it. If recurs |
| | 180 | | /// then the directory must be empty or deletion will fail. |
| | 181 | | /// </param> |
| | 182 | | /// <param name="additionalBehaviors">A collection of BatchClientBehavior instances that are applied after the C |
| | 183 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 184 | | /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</ret |
| | 185 | | public abstract Task DeleteAsync(bool? recursive = null, IEnumerable<BatchClientBehavior> additionalBehaviors = |
| | 186 | |
|
| | 187 | | /// <summary> |
| | 188 | | /// Blocking call to delete the file. |
| | 189 | | /// </summary> |
| | 190 | | /// <param name="recursive"> |
| | 191 | | /// If the file-path parameter represents a directory instead of a file, you can set the optional |
| | 192 | | /// recursive parameter to true to delete the directory and all of the files and subdirectories in it. If recurs |
| | 193 | | /// then the directory must be empty or deletion will fail. |
| | 194 | | /// </param> |
| | 195 | | /// <param name="additionalBehaviors">A collection of BatchClientBehavior instances that are applied after the C |
| | 196 | | public virtual void Delete(bool? recursive = null, IEnumerable<BatchClientBehavior> additionalBehaviors = null) |
| | 197 | | { |
| 0 | 198 | | Task asyncTask = DeleteAsync(recursive, additionalBehaviors); |
| 0 | 199 | | asyncTask.WaitAndUnaggregateException(this.CustomBehaviors, additionalBehaviors); |
| 0 | 200 | | } |
| | 201 | |
|
| | 202 | | #endregion |
| | 203 | |
|
| | 204 | | #region IRefreshable |
| | 205 | |
|
| | 206 | | /// <summary> |
| | 207 | | /// Refreshes the current <see cref="NodeFile"/>. |
| | 208 | | /// </summary> |
| | 209 | | /// <param name="detailLevel">The detail level for the refresh. If a detail level which omits the <see cref="Na |
| | 210 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 211 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 212 | | /// <returns>A <see cref="Task"/> representing the asynchronous refresh operation.</returns> |
| | 213 | | public abstract Task RefreshAsync(DetailLevel detailLevel = null, IEnumerable<BatchClientBehavior> additionalBeh |
| | 214 | |
|
| | 215 | | /// <summary> |
| | 216 | | /// Refreshes the current <see cref="NodeFile"/>. |
| | 217 | | /// </summary> |
| | 218 | | /// <param name="detailLevel">The detail level for the refresh. If a detail level which omits the <see cref="Na |
| | 219 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 220 | | public abstract void Refresh(DetailLevel detailLevel = null, IEnumerable<BatchClientBehavior> additionalBehavior |
| | 221 | |
|
| | 222 | | #endregion |
| | 223 | |
|
| | 224 | | /// <summary> |
| | 225 | | /// Encapsulates the properties of a FileItem so as to allow atomic replacement for Refresh(). |
| | 226 | | /// </summary> |
| | 227 | | internal class FileItemBox |
| | 228 | | { |
| | 229 | | protected Models.NodeFile _boundFileItem; // the protocol object to which this instance is bound |
| | 230 | | protected FileProperties _wrappedFileProperties; |
| | 231 | |
|
| 8 | 232 | | public FileItemBox(Models.NodeFile file) |
| | 233 | | { |
| 8 | 234 | | this._boundFileItem = file; |
| 8 | 235 | | if (file.Properties != null) |
| | 236 | | { |
| 6 | 237 | | this._wrappedFileProperties = new FileProperties(file.Properties); |
| | 238 | | } |
| 8 | 239 | | } |
| | 240 | |
|
| | 241 | | public bool? IsDirectory |
| | 242 | | { |
| | 243 | | get |
| | 244 | | { |
| 0 | 245 | | return _boundFileItem.IsDirectory; |
| | 246 | | } |
| | 247 | | } |
| | 248 | |
|
| | 249 | | public string Name |
| | 250 | | { |
| | 251 | | get |
| | 252 | | { |
| 6 | 253 | | return _boundFileItem.Name; |
| | 254 | | } |
| | 255 | | } |
| | 256 | |
|
| | 257 | | public FileProperties Properties |
| | 258 | | { |
| | 259 | | get |
| | 260 | | { |
| 2 | 261 | | return _wrappedFileProperties; |
| | 262 | | } |
| | 263 | | } |
| | 264 | |
|
| | 265 | | public string Url |
| | 266 | | { |
| | 267 | | get |
| | 268 | | { |
| 0 | 269 | | return _boundFileItem.Url; |
| | 270 | | } |
| | 271 | | } |
| | 272 | | } |
| | 273 | |
|
| | 274 | | } |
| | 275 | | } |