C
C#ā€¢16mo ago
Core Dream

ā” Async Method with current code.

I am having a hell of a brain fart, probably my damn ASD again. Can someone help me fix this?
private static string GetFilePath(StringBuilder builder, TrackViewModel track, string downloadPath, FileFormat fileFormat)
{
// Change ReleaseDate format to yyyy-MM-dd (Added by Core Dream Studios)

DateTime formattedDate = track.ReleaseDate;
var formattedDateString = formattedDate.ToString("yyyy-MM-dd");

builder.Clear();
builder.Append(formattedDateString);
builder.Append(" - ");
builder.Append(track.ArtistsTitle);
builder.Append(" - ");
builder.Append(track.Title);

if (!string.IsNullOrWhiteSpace(track.Version))
{
builder.Append('(');
builder.Append(track.Version);
builder.Append(')');
}

builder.Append(GetFileExtension(fileFormat));

var fileName = builder.ToString().SanitizeAsFileName();

var archivedFolderPath = $@"{downloadPath}\{formattedDateString} - {track.CatalogId} - {track.Title}";

// Create a subfolder inside the users download path based on the year, catalog id then the artist.
if (!Directory.Exists(archivedFolderPath))
Directory.CreateDirectory(archivedFolderPath);

// Added a ability to download coverart.
// Credit to @mtreit (https://github.com/treit)
// Credit to @angius (https://github.com/Atulin)


var cover = $"https://cdx.monstercat.com/?width={3000}&encoding=jpg&url=https://www.monstercat.com/release/{track.CatalogId}/cover";
DownloadCoverArtAsync(cover, archivedFolderPath);

return Path.Combine(archivedFolderPath, fileName!);
}

private static async Task DownloadCoverArtAsync(string coverURL, string downloadPath)
{
var _client = new HttpClient();
using var stream = await _client.GetStreamAsync(coverURL);
using var fs = new FileStream($@"{downloadPath}\cover.jpg", FileMode.Create);

stream.CopyTo(fs);
}
private static string GetFilePath(StringBuilder builder, TrackViewModel track, string downloadPath, FileFormat fileFormat)
{
// Change ReleaseDate format to yyyy-MM-dd (Added by Core Dream Studios)

DateTime formattedDate = track.ReleaseDate;
var formattedDateString = formattedDate.ToString("yyyy-MM-dd");

builder.Clear();
builder.Append(formattedDateString);
builder.Append(" - ");
builder.Append(track.ArtistsTitle);
builder.Append(" - ");
builder.Append(track.Title);

if (!string.IsNullOrWhiteSpace(track.Version))
{
builder.Append('(');
builder.Append(track.Version);
builder.Append(')');
}

builder.Append(GetFileExtension(fileFormat));

var fileName = builder.ToString().SanitizeAsFileName();

var archivedFolderPath = $@"{downloadPath}\{formattedDateString} - {track.CatalogId} - {track.Title}";

// Create a subfolder inside the users download path based on the year, catalog id then the artist.
if (!Directory.Exists(archivedFolderPath))
Directory.CreateDirectory(archivedFolderPath);

// Added a ability to download coverart.
// Credit to @mtreit (https://github.com/treit)
// Credit to @angius (https://github.com/Atulin)


var cover = $"https://cdx.monstercat.com/?width={3000}&encoding=jpg&url=https://www.monstercat.com/release/{track.CatalogId}/cover";
DownloadCoverArtAsync(cover, archivedFolderPath);

return Path.Combine(archivedFolderPath, fileName!);
}

private static async Task DownloadCoverArtAsync(string coverURL, string downloadPath)
{
var _client = new HttpClient();
using var stream = await _client.GetStreamAsync(coverURL);
using var fs = new FileStream($@"{downloadPath}\cover.jpg", FileMode.Create);

stream.CopyTo(fs);
}
Thanks
57 Replies
Angius
Angiusā€¢16mo ago
DownloadCoverArtAsync(cover, archivedFolderPath); await this call private static string GetFilePath(StringBuilder builder, TrackViewModel track, string downloadPath, FileFormat fileFormat) make this method async Task<string> And await it wherever you call it
Core Dream
Core DreamOPā€¢16mo ago
Core Dream
Core DreamOPā€¢16mo ago
that broke something else now šŸ˜¦ CS0104 'File' is an ambiguous reference between 'System.Net.WebRequestMethods.File' and 'System.IO.File' S
Angius
Angiusā€¢16mo ago
Remove the reference to System.Net.Web or whatever it is that brings this class
Core Dream
Core DreamOPā€¢16mo ago
Core Dream
Core DreamOPā€¢16mo ago
grrrrrr OK
Core Dream
Core DreamOPā€¢16mo ago
Core Dream
Core DreamOPā€¢16mo ago
nothing oh, i see it
Core Dream
Core DreamOPā€¢16mo ago
Core Dream
Core DreamOPā€¢16mo ago
nope
Core Dream
Core DreamOPā€¢16mo ago
Angius
Angiusā€¢16mo ago
There you have it
Core Dream
Core DreamOPā€¢16mo ago
all because of the damn Task<string>
Angius
Angiusā€¢16mo ago
Well, just await it If a method is async, await it await GetFilePath(...)
Core Dream
Core DreamOPā€¢16mo ago
Core Dream
Core DreamOPā€¢16mo ago
for fuck sake ALL I WANTED WAS A DAMN IMAGE :*(
Angius
Angiusā€¢16mo ago
What's wrong now?
Core Dream
Core DreamOPā€¢16mo ago
see the squiggly?
Angius
Angiusā€¢16mo ago
Yes, but I don't see the error message
Core Dream
Core DreamOPā€¢16mo ago
Core Dream
Core DreamOPā€¢16mo ago
THIS IS FUCKED it was compiling fine before all of this
Angius
Angiusā€¢16mo ago
Do you want help or do you just need to vent? If it's the former, provide error messages If it's the latter, let me know so I can go do some other things
Core Dream
Core DreamOPā€¢16mo ago
sorry
Core Dream
Core DreamOPā€¢16mo ago
Core Dream
Core DreamOPā€¢16mo ago
Core Dream
Core DreamOPā€¢16mo ago
this
Angius
Angiusā€¢16mo ago
Well, your method returns a Task it seems void -> Task T -> Task<T>
Core Dream
Core DreamOPā€¢16mo ago
i will repost code since we changed 100 things
Angius
Angiusā€¢16mo ago
Right now the method is akin to void, but you're trying to return a string from it
Core Dream
Core DreamOPā€¢16mo ago
private static async Task DownloadCoverArtAsync(string coverURL, string downloadPath)
{
var _client = new HttpClient();
using var stream = await _client.GetStreamAsync(coverURL);
using var fs = new FileStream($@"{downloadPath}\cover.jpg", FileMode.Create);

stream.CopyTo(fs);
}


private static async Task GetFilePath(StringBuilder builder, TrackViewModel track, string downloadPath, FileFormat fileFormat)
{
// Change ReleaseDate format to yyyy-MM-dd (Added by Core Dream Studios)

DateTime formattedDate = track.ReleaseDate;
var formattedDateString = formattedDate.ToString("yyyy-MM-dd");

builder.Clear();
builder.Append(formattedDateString);
builder.Append(" - ");
builder.Append(track.ArtistsTitle);
builder.Append(" - ");
builder.Append(track.Title);

if (!string.IsNullOrWhiteSpace(track.Version))
{
builder.Append('(');
builder.Append(track.Version);
builder.Append(')');
}

builder.Append(GetFileExtension(fileFormat));

var fileName = builder.ToString().SanitizeAsFileName();

var archivedFolderPath = $@"{downloadPath}\{formattedDateString} - {track.CatalogId} - {track.Title}";

// Create a subfolder inside the users download path based on the year, catalog id then the artist.
if (!Directory.Exists(archivedFolderPath))
Directory.CreateDirectory(archivedFolderPath);

// Added a ability to download coverart.
// Credit to @mtreit (https://github.com/treit)
// Credit to @angius (https://github.com/Atulin)


var cover = $"https://cdx.monstercat.com/?width={3000}&encoding=jpg&url=https://www.monstercat.com/release/{track.CatalogId}/cover";
await DownloadCoverArtAsync(cover, archivedFolderPath);

return Path.Combine(archivedFolderPath, fileName!);
}
private static async Task DownloadCoverArtAsync(string coverURL, string downloadPath)
{
var _client = new HttpClient();
using var stream = await _client.GetStreamAsync(coverURL);
using var fs = new FileStream($@"{downloadPath}\cover.jpg", FileMode.Create);

stream.CopyTo(fs);
}


private static async Task GetFilePath(StringBuilder builder, TrackViewModel track, string downloadPath, FileFormat fileFormat)
{
// Change ReleaseDate format to yyyy-MM-dd (Added by Core Dream Studios)

DateTime formattedDate = track.ReleaseDate;
var formattedDateString = formattedDate.ToString("yyyy-MM-dd");

builder.Clear();
builder.Append(formattedDateString);
builder.Append(" - ");
builder.Append(track.ArtistsTitle);
builder.Append(" - ");
builder.Append(track.Title);

if (!string.IsNullOrWhiteSpace(track.Version))
{
builder.Append('(');
builder.Append(track.Version);
builder.Append(')');
}

builder.Append(GetFileExtension(fileFormat));

var fileName = builder.ToString().SanitizeAsFileName();

var archivedFolderPath = $@"{downloadPath}\{formattedDateString} - {track.CatalogId} - {track.Title}";

// Create a subfolder inside the users download path based on the year, catalog id then the artist.
if (!Directory.Exists(archivedFolderPath))
Directory.CreateDirectory(archivedFolderPath);

// Added a ability to download coverart.
// Credit to @mtreit (https://github.com/treit)
// Credit to @angius (https://github.com/Atulin)


var cover = $"https://cdx.monstercat.com/?width={3000}&encoding=jpg&url=https://www.monstercat.com/release/{track.CatalogId}/cover";
await DownloadCoverArtAsync(cover, archivedFolderPath);

return Path.Combine(archivedFolderPath, fileName!);
}
latest
Angius
Angiusā€¢16mo ago
It is exactly as I said
Core Dream
Core DreamOPā€¢16mo ago
yes i NEED the string in order to do the file saving
Angius
Angiusā€¢16mo ago
So... return a Task<string> Not a Task
Core Dream
Core DreamOPā€¢16mo ago
oh so put return Task<string> Path.Combine(archivedFolderPath, fileName!); ?
Angius
Angiusā€¢16mo ago
No
Core Dream
Core DreamOPā€¢16mo ago
if I do Task<string> it breaks the entire app
Angius
Angiusā€¢16mo ago
Change the return type of this method
Core Dream
Core DreamOPā€¢16mo ago
this is not m,y app
Angius
Angiusā€¢16mo ago
From Task to Task<string>
Core Dream
Core DreamOPā€¢16mo ago
ok
Core Dream
Core DreamOPā€¢16mo ago
Core Dream
Core DreamOPā€¢16mo ago
ok well im just gonna remove the ability for cover art im not even getting paid šŸ˜¦ everything is broken when I use Task<string>?
Angius
Angiusā€¢16mo ago
Well, the last two errors are obvious at least Wherever you call this method, you don't await it Asynchronous code is "infectious"
Core Dream
Core DreamOPā€¢16mo ago
i dont even need it awaited/async its ONE image
Angius
Angiusā€¢16mo ago
Once something is async, the entire calling chain needs to be async all the way down to Main
Core Dream
Core DreamOPā€¢16mo ago
im not downloading wikipedia here
Angius
Angiusā€¢16mo ago
Just use synchronous versions of those methods then ĀÆ\_(惄)_/ĀÆ HttpClient should have a synchronous GetStream method
Core Dream
Core DreamOPā€¢16mo ago
nope, ilooked
Core Dream
Core DreamOPā€¢16mo ago
Core Dream
Core DreamOPā€¢16mo ago
somehow that goddamn code broke this too and i dont know mvvm so im not touching this im just removing cover download they can add their own code its call gitHUB for a reason
mtreit
mtreitā€¢16mo ago
You want a synchronous version of downloading a file with HttpClient?
Core Dream
Core DreamOPā€¢16mo ago
yes šŸ˜¦
mtreit
mtreitā€¢16mo ago
var uri = "https://treit.github.io/images/bdn1.jpg";
var client = new HttpClient();
var msg = new HttpRequestMessage(HttpMethod.Get, uri);
using var response = client.Send(msg);
using var fs = new FileStream(@"c:\temp\bdn1.jpg", FileMode.Create);
var stream = response.Content.ReadAsStream();
stream.CopyTo(fs);
var uri = "https://treit.github.io/images/bdn1.jpg";
var client = new HttpClient();
var msg = new HttpRequestMessage(HttpMethod.Get, uri);
using var response = client.Send(msg);
using var fs = new FileStream(@"c:\temp\bdn1.jpg", FileMode.Create);
var stream = response.Content.ReadAsStream();
stream.CopyTo(fs);
Core Dream
Core DreamOPā€¢16mo ago
i appreciate it
mtreit
mtreitā€¢16mo ago
Here you go
Core Dream
Core DreamOPā€¢16mo ago
oh didn't know you could use HttpRequestMessage gonna add that for homework I need some air, letting my BP control my emotions heh I appreciate the non async version though if it was my project, id do it that way but this was someone elses hug @ZZZZZZZZZZZZZZZZZZZZZZZZZ and @mtreit i feel better now, took some water too heh i need to relax and try to google some more too
Accord
Accordā€¢16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server