C
C#ā€¢11mo ago
Core Dream Studios

ā” 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ā€¢11mo 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 Studios
Core Dream Studiosā€¢11mo ago
Core Dream Studios
Core Dream Studiosā€¢11mo ago
that broke something else now šŸ˜¦ CS0104 'File' is an ambiguous reference between 'System.Net.WebRequestMethods.File' and 'System.IO.File' S
Angius
Angiusā€¢11mo ago
Remove the reference to System.Net.Web or whatever it is that brings this class
Core Dream Studios
Core Dream Studiosā€¢11mo ago
Core Dream Studios
Core Dream Studiosā€¢11mo ago
grrrrrr OK
Core Dream Studios
Core Dream Studiosā€¢11mo ago
Core Dream Studios
Core Dream Studiosā€¢11mo ago
nothing oh, i see it
Core Dream Studios
Core Dream Studiosā€¢11mo ago
Core Dream Studios
Core Dream Studiosā€¢11mo ago
nope
Core Dream Studios
Core Dream Studiosā€¢11mo ago
Angius
Angiusā€¢11mo ago
There you have it
Core Dream Studios
Core Dream Studiosā€¢11mo ago
all because of the damn Task<string>
Angius
Angiusā€¢11mo ago
Well, just await it If a method is async, await it await GetFilePath(...)
Core Dream Studios
Core Dream Studiosā€¢11mo ago
Core Dream Studios
Core Dream Studiosā€¢11mo ago
for fuck sake ALL I WANTED WAS A DAMN IMAGE :*(
Angius
Angiusā€¢11mo ago
What's wrong now?
Core Dream Studios
Core Dream Studiosā€¢11mo ago
see the squiggly?
Angius
Angiusā€¢11mo ago
Yes, but I don't see the error message
Core Dream Studios
Core Dream Studiosā€¢11mo ago
Core Dream Studios
Core Dream Studiosā€¢11mo ago
THIS IS FUCKED it was compiling fine before all of this
Angius
Angiusā€¢11mo 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 Studios
Core Dream Studiosā€¢11mo ago
sorry
Core Dream Studios
Core Dream Studiosā€¢11mo ago
Core Dream Studios
Core Dream Studiosā€¢11mo ago
Core Dream Studios
Core Dream Studiosā€¢11mo ago
this
Angius
Angiusā€¢11mo ago
Well, your method returns a Task it seems void -> Task T -> Task<T>
Core Dream Studios
Core Dream Studiosā€¢11mo ago
i will repost code since we changed 100 things
Angius
Angiusā€¢11mo ago
Right now the method is akin to void, but you're trying to return a string from it
Core Dream Studios
Core Dream Studiosā€¢11mo 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ā€¢11mo ago
It is exactly as I said
Core Dream Studios
Core Dream Studiosā€¢11mo ago
yes i NEED the string in order to do the file saving
Angius
Angiusā€¢11mo ago
So... return a Task<string> Not a Task
Core Dream Studios
Core Dream Studiosā€¢11mo ago
oh so put return Task<string> Path.Combine(archivedFolderPath, fileName!); ?
Angius
Angiusā€¢11mo ago
No
Core Dream Studios
Core Dream Studiosā€¢11mo ago
if I do Task<string> it breaks the entire app
Angius
Angiusā€¢11mo ago
Change the return type of this method
Core Dream Studios
Core Dream Studiosā€¢11mo ago
this is not m,y app
Angius
Angiusā€¢11mo ago
From Task to Task<string>
Core Dream Studios
Core Dream Studiosā€¢11mo ago
ok
Core Dream Studios
Core Dream Studiosā€¢11mo ago
Core Dream Studios
Core Dream Studiosā€¢11mo 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ā€¢11mo 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 Studios
Core Dream Studiosā€¢11mo ago
i dont even need it awaited/async its ONE image
Angius
Angiusā€¢11mo ago
Once something is async, the entire calling chain needs to be async all the way down to Main
Core Dream Studios
Core Dream Studiosā€¢11mo ago
im not downloading wikipedia here
Angius
Angiusā€¢11mo ago
Just use synchronous versions of those methods then ĀÆ\_(惄)_/ĀÆ HttpClient should have a synchronous GetStream method
Core Dream Studios
Core Dream Studiosā€¢11mo ago
nope, ilooked
Core Dream Studios
Core Dream Studiosā€¢11mo ago
Core Dream Studios
Core Dream Studiosā€¢11mo 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ā€¢11mo ago
You want a synchronous version of downloading a file with HttpClient?
Core Dream Studios
Core Dream Studiosā€¢11mo ago
yes šŸ˜¦
mtreit
mtreitā€¢11mo 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 Studios
Core Dream Studiosā€¢11mo ago
i appreciate it
mtreit
mtreitā€¢11mo ago
Here you go
Core Dream Studios
Core Dream Studiosā€¢11mo 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ā€¢11mo 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
More Posts
ā” Assistance required trying to create a generic base class for unit tests for mocked servicesFor some context, I started with a net 6 console app (you know the template that just acts as Main?)ā” Avalonia nested ItemsRepeater datatemplatesI am attempting to handle rendering UI for a list of lists of objects that implement a base interfacā” Need help creating Alarm Clocks for an ApplicationI need help adding a renaming functionality to my Alarms so that the user can change the name when tā” Apache vs NginxWhich one would you prefer for hosting websites?ā” How To Refresh ContentPage ControlsWhen routing from the MainPage to my details page I pass in: ``` MyData myData = currentSelectedItemā” blazor server app Authentication cookie or jwt token?Iā€™m trying to build a login authentication with email only. I want them to be authenticated that expāœ… RichTextBox - Cannot type textHello I creating WPF Application with rich text box and when I put him there from toolbox the input āœ… EF Core, Proper DbContext InstantiationWhen instantiating the DbContext, I've always created it in a Repository class (not a generic reposiāœ… Failed to bind to address http://127.0.0.1:5196: address already in useHi firends, I'm working on a project using Microservices, I use `RabbitMQ` as the message broker, anāœ… #async #Unity how to do something after tasks were complete and not block main threads with tasksi have tasks that can take very log time to complete in unity and at the moment i launch tasks thi