chore: document ImageManager
This commit is contained in:
@@ -7,9 +7,14 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace CarManagerV3
|
namespace CarManagerV3
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The <c>ImageManager</c> class is responsible for managing car images, including fetching and storing them locally.
|
||||||
|
/// </summary>
|
||||||
internal class ImageManager
|
internal class ImageManager
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes the image folder by creating it if it does not exist.
|
||||||
|
/// </summary>
|
||||||
public static void InitializeImageFolder()
|
public static void InitializeImageFolder()
|
||||||
{
|
{
|
||||||
string path = "images";
|
string path = "images";
|
||||||
@@ -19,6 +24,11 @@ namespace CarManagerV3
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generates the image path for a given car based on its attributes.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="car">The car.</param>
|
||||||
|
/// <returns>The image path for this Car.</returns>
|
||||||
public static string GetImagePath(Car car)
|
public static string GetImagePath(Car car)
|
||||||
{
|
{
|
||||||
string basePath = "images/";
|
string basePath = "images/";
|
||||||
@@ -26,6 +36,11 @@ namespace CarManagerV3
|
|||||||
return basePath + fileName;
|
return basePath + fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the image for a given car, fetching it if necessary.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="car">The car.</param>
|
||||||
|
/// <returns>The <see cref="Image"/> object for the car.</returns>
|
||||||
public static Image GetImage(Car car)
|
public static Image GetImage(Car car)
|
||||||
{
|
{
|
||||||
InitializeImageFolder();
|
InitializeImageFolder();
|
||||||
@@ -43,11 +58,16 @@ namespace CarManagerV3
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches an image for a Car from https://cdn.imagin.studio/getimage if it does not already exist locally and saves it to images/<c>Make_Model_Year_Color.webp</>.
|
||||||
|
/// If the image cannot be fetched, a placeholder image is used instead.
|
||||||
|
/// Uses example customer id "hrjavascript-mastery", because they wouldn't give me one. Stole this from a tutorial page. :)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="car">The car.</param>
|
||||||
public static void FetchImage(Car car)
|
public static void FetchImage(Car car)
|
||||||
{
|
{
|
||||||
// Fetch the image from https://cdn.imagin.studio/getimage and save it to images/Make_Model_Year.webp
|
// Fetch the image from https://cdn.imagin.studio/getimage and save it to images/Make_Model_Year.webp
|
||||||
// use params like this: ?customer=hrjavascript-mastery&zoomType=fullscreen&make={make}&modelFamily={model}&modelYear={year}&angle=front&paintDescription={color}&fileType=png
|
// use params like this: ?customer=hrjavascript-mastery&zoomType=fullscreen&make={make}&modelFamily={model}&modelYear={year}&angle=front&paintDescription={color}&fileType=png
|
||||||
|
|
||||||
// check if the image already exists
|
// check if the image already exists
|
||||||
string path = GetImagePath(car);
|
string path = GetImagePath(car);
|
||||||
if (System.IO.File.Exists(path))
|
if (System.IO.File.Exists(path))
|
||||||
@@ -55,7 +75,7 @@ namespace CarManagerV3
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
string url = $"https://cdn.imagin.studio/getimage?customer=hrjavascript-mastery&zoomType=fullscreen&make={car.Make}&modelFamily={car.Model}&modelYear={car.Year}&angle=front&paintDescription={car.Color}&fileType=png";
|
string url = $"https://cdn.imagin.studio/getimage?customer=hrjavascript-mastery&zoomType=fullscreen&make={car.Make}&modelFamily={car.Model}&modelYear={car.Year}&angle=front&paintDescription={car.Color}&fileType=png";
|
||||||
//add Referer header
|
//add Referer header to avoid 403 error
|
||||||
using (var client = new System.Net.WebClient())
|
using (var client = new System.Net.WebClient())
|
||||||
{
|
{
|
||||||
client.Headers.Add("Referer", "http://localhost");
|
client.Headers.Add("Referer", "http://localhost");
|
||||||
@@ -65,7 +85,7 @@ namespace CarManagerV3
|
|||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
// if error, use no_image_available.png
|
// if error, use fallback image no_image_available.png
|
||||||
System.IO.File.Copy("images/no_image_available.png", path);
|
System.IO.File.Copy("images/no_image_available.png", path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user