// This method is used to store vehicle photos to the server machine // and insert the data into Photos table. public async Task <Photo> UploadPhoto(Vehicle vehicle, IFormFile formFile, byte[] formFileContent, string targetFilePath) { // Create a folder to store vehicle folders individually. Every vehicle photos will be stored in the folder // named by the vehicle id to reduce the chance to be in the same folder with the file with same name. var photoDirectory = Path.Combine(FilePaths.VehiclePhotosDirectory, vehicle.Id.ToString()); var physicalStoragePath = Path.Combine(targetFilePath, photoDirectory); if (!Directory.Exists(physicalStoragePath)) { Directory.CreateDirectory(physicalStoragePath); } // Store the file into the server machine and return the file name in the server machine. var fileName = await _photoStorage.StorePhoto(physicalStoragePath, formFile, formFileContent); // Create thumbnail for vehicle image and save it to the server. var originImageFilePath = Path.Combine(physicalStoragePath, fileName); var thumbNailFilePath = _photoStorage.StoreThumbnail(originImageFilePath); // Write into database after the file is uploaded successfully. var photo = new Photo { FileName = fileName, Thumbnail = Path.GetFileName(thumbNailFilePath) }; vehicle.Photos.Add(photo); await _unitOfWork.CompleteAsync(); return(photo); }