@@ -986,21 +986,23 @@ private static async Task<bool> PutRequestAsync(string url, string filePath, boo
986
986
SetDefaultHeaders ( contentHeaders ) ;
987
987
988
988
FileInfo fileInfo = new FileInfo ( filePath ) ;
989
- FileStream fileStream = fileInfo . Open ( FileMode . Open , FileAccess . Read ) ;
990
- HttpContent httpContent = new StreamContent ( fileStream ) ;
991
- if ( isManifest )
989
+ using ( FileStream fileStream = fileInfo . Open ( FileMode . Open , FileAccess . Read ) )
992
990
{
993
- httpContent . Headers . Add ( "Content-Type" , "application/vnd.oci.image.manifest.v1+json" ) ;
994
- }
995
- else
996
- {
997
- httpContent . Headers . Add ( "Content-Type" , "application/octet-stream" ) ;
998
- }
991
+ HttpContent httpContent = new StreamContent ( fileStream ) ;
992
+ if ( isManifest )
993
+ {
994
+ httpContent . Headers . Add ( "Content-Type" , "application/vnd.oci.image.manifest.v1+json" ) ;
995
+ }
996
+ else
997
+ {
998
+ httpContent . Headers . Add ( "Content-Type" , "application/octet-stream" ) ;
999
+ }
999
1000
1000
- HttpResponseMessage response = await s_client . PutAsync ( url , httpContent ) ;
1001
- response . EnsureSuccessStatusCode ( ) ;
1002
- fileStream . Close ( ) ;
1003
- return response . IsSuccessStatusCode ;
1001
+ HttpResponseMessage response = await s_client . PutAsync ( url , httpContent ) ;
1002
+ response . EnsureSuccessStatusCode ( ) ;
1003
+
1004
+ return response . IsSuccessStatusCode ;
1005
+ }
1004
1006
}
1005
1007
catch ( HttpRequestException e )
1006
1008
{
@@ -1063,15 +1065,36 @@ internal bool PushNupkgACR(string psd1OrPs1File, string outputNupkgDir, string p
1063
1065
_cmdletPassedIn . WriteVerbose ( "Finish uploading blob" ) ;
1064
1066
bool moduleUploadSuccess = EndUploadBlob ( registry , moduleLocation , fullNupkgFile , nupkgDigest , false , acrAccessToken ) . Result ;
1065
1067
1068
+ /* Upload an empty file-- needed by ACR server */
1069
+ _cmdletPassedIn . WriteVerbose ( "Create an empty file" ) ;
1070
+ string emptyFileName = "empty.txt" ;
1071
+ var emptyFilePath = System . IO . Path . Combine ( outputNupkgDir , emptyFileName ) ;
1072
+ // Rename the empty file in case such a file already exists in the temp folder (although highly unlikely)
1073
+ while ( File . Exists ( emptyFilePath ) )
1074
+ {
1075
+ emptyFilePath = Guid . NewGuid ( ) . ToString ( ) + ".txt" ;
1076
+ }
1077
+ using ( FileStream configStream = new FileStream ( emptyFilePath , FileMode . Create ) ) { }
1078
+ _cmdletPassedIn . WriteVerbose ( "Start uploading an empty file" ) ;
1079
+ var emptyLocation = GetStartUploadBlobLocation ( registry , pkgName , acrAccessToken ) . Result ;
1080
+ _cmdletPassedIn . WriteVerbose ( "Computing digest for empty file" ) ;
1081
+ bool emptyDigestCreated = CreateDigest ( emptyFilePath , out string emptyDigest , out ErrorRecord emptyDigestError ) ;
1082
+ if ( ! emptyDigestCreated )
1083
+ {
1084
+ _cmdletPassedIn . ThrowTerminatingError ( emptyDigestError ) ;
1085
+ }
1086
+ _cmdletPassedIn . WriteVerbose ( "Finish uploading empty file" ) ;
1087
+ bool emptyFileUploadSuccess = EndUploadBlob ( registry , emptyLocation , emptyFilePath , emptyDigest , false , acrAccessToken ) . Result ;
1088
+
1089
+ /* Create config layer */
1066
1090
_cmdletPassedIn . WriteVerbose ( "Create the config file" ) ;
1067
1091
string configFileName = "config.json" ;
1068
1092
var configFilePath = System . IO . Path . Combine ( outputNupkgDir , configFileName ) ;
1069
1093
while ( File . Exists ( configFilePath ) )
1070
1094
{
1071
1095
configFilePath = Guid . NewGuid ( ) . ToString ( ) + ".json" ;
1072
1096
}
1073
- FileStream configStream = File . Create ( configFilePath ) ;
1074
- configStream . Close ( ) ;
1097
+ using ( FileStream configStream = new FileStream ( configFilePath , FileMode . Create ) ) { }
1075
1098
_cmdletPassedIn . WriteVerbose ( "Computing digest for config" ) ;
1076
1099
bool configDigestCreated = CreateDigest ( configFilePath , out string configDigest , out ErrorRecord configDigestError ) ;
1077
1100
if ( ! configDigestCreated )
@@ -1163,36 +1186,36 @@ private bool CreateDigest(string fileName, out string digest, out ErrorRecord er
1163
1186
{
1164
1187
FileInfo fileInfo = new FileInfo ( fileName ) ;
1165
1188
SHA256 mySHA256 = SHA256 . Create ( ) ;
1166
- FileStream fileStream = fileInfo . Open ( FileMode . Open , FileAccess . Read ) ;
1167
- digest = string . Empty ;
1168
-
1169
- try
1170
- {
1171
- // Create a fileStream for the file.
1172
- // Be sure it's positioned to the beginning of the stream.
1173
- fileStream . Position = 0 ;
1174
- // Compute the hash of the fileStream.
1175
- byte [ ] hashValue = mySHA256 . ComputeHash ( fileStream ) ;
1176
- StringBuilder stringBuilder = new StringBuilder ( ) ;
1177
- foreach ( byte b in hashValue )
1178
- stringBuilder . AppendFormat ( "{0:x2}" , b ) ;
1179
- digest = stringBuilder . ToString ( ) ;
1180
- // Write the name and hash value of the file to the console.
1181
- _cmdletPassedIn . WriteVerbose ( $ "{ fileInfo . Name } : { hashValue } ") ;
1182
- error = null ;
1183
- }
1184
- catch ( IOException ex )
1185
- {
1186
- var IOError = new ErrorRecord ( ex , $ "IOException for .nupkg file: { ex . Message } ", ErrorCategory . InvalidOperation , null ) ;
1187
- error = IOError ;
1188
- }
1189
- catch ( UnauthorizedAccessException ex )
1189
+ using ( FileStream fileStream = fileInfo . Open ( FileMode . Open , FileAccess . Read ) )
1190
1190
{
1191
- var AuthorizationError = new ErrorRecord ( ex , $ "UnauthorizedAccessException for .nupkg file: { ex . Message } ", ErrorCategory . PermissionDenied , null ) ;
1192
- error = AuthorizationError ;
1193
- }
1191
+ digest = string . Empty ;
1194
1192
1195
- fileStream . Close ( ) ;
1193
+ try
1194
+ {
1195
+ // Create a fileStream for the file.
1196
+ // Be sure it's positioned to the beginning of the stream.
1197
+ fileStream . Position = 0 ;
1198
+ // Compute the hash of the fileStream.
1199
+ byte [ ] hashValue = mySHA256 . ComputeHash ( fileStream ) ;
1200
+ StringBuilder stringBuilder = new StringBuilder ( ) ;
1201
+ foreach ( byte b in hashValue )
1202
+ stringBuilder . AppendFormat ( "{0:x2}" , b ) ;
1203
+ digest = stringBuilder . ToString ( ) ;
1204
+ // Write the name and hash value of the file to the console.
1205
+ _cmdletPassedIn . WriteVerbose ( $ "{ fileInfo . Name } : { hashValue } ") ;
1206
+ error = null ;
1207
+ }
1208
+ catch ( IOException ex )
1209
+ {
1210
+ var IOError = new ErrorRecord ( ex , $ "IOException for .nupkg file: { ex . Message } ", ErrorCategory . InvalidOperation , null ) ;
1211
+ error = IOError ;
1212
+ }
1213
+ catch ( UnauthorizedAccessException ex )
1214
+ {
1215
+ var AuthorizationError = new ErrorRecord ( ex , $ "UnauthorizedAccessException for .nupkg file: { ex . Message } ", ErrorCategory . PermissionDenied , null ) ;
1216
+ error = AuthorizationError ;
1217
+ }
1218
+ }
1196
1219
if ( error != null )
1197
1220
{
1198
1221
return false ;
0 commit comments