Skip to content

Commit 11b9d5d

Browse files
authored
Publish fixes (PowerShell#1545)
1 parent 8695ac9 commit 11b9d5d

File tree

1 file changed

+66
-43
lines changed

1 file changed

+66
-43
lines changed

src/code/ACRServerAPICalls.cs

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -986,21 +986,23 @@ private static async Task<bool> PutRequestAsync(string url, string filePath, boo
986986
SetDefaultHeaders(contentHeaders);
987987

988988
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))
992990
{
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+
}
9991000

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+
}
10041006
}
10051007
catch (HttpRequestException e)
10061008
{
@@ -1063,15 +1065,36 @@ internal bool PushNupkgACR(string psd1OrPs1File, string outputNupkgDir, string p
10631065
_cmdletPassedIn.WriteVerbose("Finish uploading blob");
10641066
bool moduleUploadSuccess = EndUploadBlob(registry, moduleLocation, fullNupkgFile, nupkgDigest, false, acrAccessToken).Result;
10651067

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 */
10661090
_cmdletPassedIn.WriteVerbose("Create the config file");
10671091
string configFileName = "config.json";
10681092
var configFilePath = System.IO.Path.Combine(outputNupkgDir, configFileName);
10691093
while (File.Exists(configFilePath))
10701094
{
10711095
configFilePath = Guid.NewGuid().ToString() + ".json";
10721096
}
1073-
FileStream configStream = File.Create(configFilePath);
1074-
configStream.Close();
1097+
using (FileStream configStream = new FileStream(configFilePath, FileMode.Create)){ }
10751098
_cmdletPassedIn.WriteVerbose("Computing digest for config");
10761099
bool configDigestCreated = CreateDigest(configFilePath, out string configDigest, out ErrorRecord configDigestError);
10771100
if (!configDigestCreated)
@@ -1163,36 +1186,36 @@ private bool CreateDigest(string fileName, out string digest, out ErrorRecord er
11631186
{
11641187
FileInfo fileInfo = new FileInfo(fileName);
11651188
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))
11901190
{
1191-
var AuthorizationError = new ErrorRecord(ex, $"UnauthorizedAccessException for .nupkg file: {ex.Message}", ErrorCategory.PermissionDenied, null);
1192-
error = AuthorizationError;
1193-
}
1191+
digest = string.Empty;
11941192

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+
}
11961219
if (error != null)
11971220
{
11981221
return false;

0 commit comments

Comments
 (0)