Array.map vs Array.forEach

If you are working with JavaScript, then you must have come across these two similar looking array methods: Array.prototype.map() and Array.prototype.forEach().

Most of the developers thinks that Array.map function and forEach for an Array is the same, but it is not. Lets see some examples:
1. Array.prototype.forEach
forEach can be used just like a traditional ‘for loop’ that iterates each of the array element once.

let arrayElements = ['A','C','D'];  
arrayElements.forEach((ele)=>{  
  console.log(ele);  
 })  

The above example will generate the following –
A
C
D

2. Array.prototype.map
The map() function creates a new Array with the results of calling a function for every element of the calling array.

let arrayElements = [1, 4, 6];  
var result = arrayElements .map((ele)=>{  
  return (ele * 2);  
});  
console.log(result);  

The output of the code is:
[ 2, 8, 12 ]
In the above example we are iterating through each element of Array and multiplying it by 2.

Create folders & upload images under Media in Umbraco

This is a continuation from my previous post about downloading and uploading images to media programmatically, via code.

This example code will help you create folders under Media in the following hierarchy –

Video Images

-> 2018 (current year)

-> November (current month)

public class ImageService
{
  private readonly IUmbracoContextWrapper _umbCtxtHelper;
  private readonly IMediaService _mediaService;

  public ImageService(IUmbracoContextWrapper umbCtxtHelper)
  {
     _umbCtxtHelper = umbCtxtHelper;
     _mediaService = _umbCtxtHelper.MediaService;
  }  

  public int UploadMedia(string imageSrc)
  {
   IMedia newImage = null;
   try
   {
     var mediaSvc = _mediaService;
     var mediaExtension = Path.GetExtension(imageSrc);
     var imageName = Guid.NewGuid() + mediaExtension;

     var image = DownloadImageFromUrl(imageSrc);

     const string rootPath = "~\\media";
     var fileName = 
     HttpContext.Current.Server.MapPath(Path.Combine(rootPath, imageName));
     image.Save(fileName);

     // Here we are creating the folders and adding images to them
     var parentFolderId = GetFolderId();
     newImage = mediaSvc.CreateMedia(imageName, parentFolderId, "Image");

     var buffer = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath("~\\media\\" + imageName));

     newImage.SetValue("umbracoFile", imageName, new MemoryStream(buffer));
     mediaSvc.Save(newImage);

     if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~\\media\\" + imageName)))
     {
       System.IO.File.Delete(HttpContext.Current.Server.MapPath("~\\media\\" + imageName));
      }
   }
   catch (Exception ex)
   {
     // log the exception
   }
   return newImage.Id;
  }
private System.Drawing.Image DownloadImageFromUrl(string imageUrl)
  {
   System.Drawing.Image image = null;
   try
   {
     var webRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
     webRequest.AllowWriteStreamBuffering = true;
     webRequest.Timeout = 30000;

     var webResponse = webRequest.GetResponse();
     var stream = webResponse.GetResponseStream();
     image = System.Drawing.Image.FromStream(stream);

     webResponse.Close();
   }
   catch (Exception ex)
   {
     // log the exception
   }
   return image;
  }   
private static int GetFolderId()
  {
    var currentYear = DateTime.Now.Year.ToString();
    var currentMonth = DateTime.Now.ToString("MMMM", CultureInfo.InvariantCulture);

    var parentFolder = GetParentFolder("Video Images")
                                  ?? CreateAndSaveMediaFolder("Video Images", -1);

    var yearFolder = GetSubFolder(parentFolder.Id, currentYear)
                                ?? CreateAndSaveMediaFolder(currentYear, genreFolder.Id);

    var monthFolder = GetSubFolder(yearFolder.Id, currentMonth)
                                 ?? CreateAndSaveMediaFolder(currentMonth, yearFolder.Id);

    return monthFolder.Id;
  }
private static IMedia CreateAndSaveMediaFolder(string folderName, int parentFolderId)
  {
    var folder = _mediaService.CreateMedia(folderName, parentFolderId, "Folder");
    _mediaService.Save(folder);
    return folder;
  }
private IMedia GetSubFolder(int parentFolderId, string folderName)
        {           
            var folder = _mediaService.GetChildren(parentFolderId)
                .Where(c => c.ContentType.Name == "Folder" && c.Name.Equals(folderName, StringComparison.OrdinalIgnoreCase))
                .Select(f => f).FirstOrDefault();

            return folder;
        }
private IMedia GetParentFolder(string folderName)
        {          
            var folder = _mediaService.GetRootMedia()
                .Where(m => m.ContentType.Name == "Folder" && m.Name.Equals(folderName, StringComparison.OrdinalIgnoreCase))
                .Select(y => y).FirstOrDefault();

            return folder;
        }
public interface IUmbracoContextWrapper
{
   IMediaService MediaService { get; }
}

Extended Gaming Mouse Pad, VicTsing Large Size Mouse and Keyboard Mat for Gaming and Working – 80x40cm (31.5×15.75in)


Sandisk SDSQXA1-128G-GN6MA Extreme 128GB microSD UHS-I Card Adapter 160MB/s U3 A2, Black

Disclosure: We use affiliate links to monetize our content.  We may receive a commission on products or services that you purchase through clicking on links within this blog.

Upload images to Umbraco

It is easy to upload images to Media folder in Umbraco but there are some instances where you need to upload images to media folder programatically, via code.

Here is the code to do that. You can use this code to download and upload any image.


public class ImageService
{
  private readonly IUmbracoContextWrapper _umbCtxtHelper;
private readonly IMediaService _mediaService;

  public ImageService(IUmbracoContextWrapper umbCtxtHelper)
  {
     _umbCtxtHelper = umbCtxtHelper;
_mediaService = _umbCtxtHelper.MediaService;
  }  

  public int UploadMedia(string imageSrc)
  {
   IMedia newImage = null;
   try
   {
var mediaSvc = _mediaService;
     var mediaExtension = Path.GetExtension(imageSrc);
     var imageName = Guid.NewGuid() + mediaExtension;

     var image = DownloadImageFromUrl(imageSrc);

     const string rootPath = "~\\media";
     var fileName = HttpContext.Current.Server.MapPath(Path.Combine(rootPath, imageName));
     image.Save(fileName);

     // -1 is the root folderID, i.e. Media.
     // All images will be saved under Media in Umbraco
     newImage = mediaSvc.CreateMedia(imageName, -1, "Image");
     var buffer = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath("~\\media\\" + imageName));

     newImage.SetValue("umbracoFile", imageName, new MemoryStream(buffer));
     mediaSvc.Save(newImage);

     if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~\\media\\" + imageName)))
     {
       System.IO.File.Delete(HttpContext.Current.Server.MapPath("~\\media\\" + imageName));
      }
   }
   catch (Exception ex)
   {
     // log the exception
   }

   return newImage.Id;
  }

  private System.Drawing.Image DownloadImageFromUrl(string imageUrl)
  {
   System.Drawing.Image image = null;
   try
   {
     var webRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
     webRequest.AllowWriteStreamBuffering = true;
     webRequest.Timeout = 30000;

     var webResponse = webRequest.GetResponse();
     var stream = webResponse.GetResponseStream();
     image = System.Drawing.Image.FromStream(stream);

     webResponse.Close();
   }
   catch (Exception ex)
   {
     // log the exception
   }
   return image;
  }
}

public interface IUmbracoContextWrapper
{
   IMediaService MediaService { get; }
}

If you want to create folders under Media and upload images to those in Umbraco. Read this post here

Date Formats in SQL Server

1) SELECT CONVERT(VARCHAR,GETDATE(),100)

Result
May 13 2012 11:30AM

2) SELECT CONVERT(VARCHAR,GETDATE(),101)

Result
05/13/2012

3) SELECT CONVERT(VARCHAR,GETDATE(),102)

Result
2012.05.13

4) SELECT CONVERT(VARCHAR,GETDATE(),103)

Result
13/05/2012

5) SELECT CONVERT(VARCHAR,GETDATE(),104)

Result
13.05.2012

6) SELECT CONVERT(VARCHAR,GETDATE(),105)

Result
13-05-2012

7) SELECT CONVERT(VARCHAR,GETDATE(),106)

Result
13 May 2012

8) SELECT CONVERT(VARCHAR,GETDATE(),107)

Result
May 13, 2012

9) SELECT CONVERT(VARCHAR,GETDATE(),108)

Result
11:41:51

10) SELECT CONVERT(VARCHAR,GETDATE(),109)

Result
May 13 2012 11:43:04:267AM

11) SELECT CONVERT(VARCHAR,GETDATE(),110)

Result
05-13-2012

12) SELECT CONVERT(VARCHAR,GETDATE(),111)

Result
2012/05/13

13) SELECT CONVERT(VARCHAR,GETDATE(),112)

Result
20120513

14) SELECT CONVERT(VARCHAR,GETDATE(),113)

Result
13 May 2012 11:50:02:510

15) SELECT CONVERT(VARCHAR,GETDATE(),114)

Result
11:51:00:330

16) SELECT CONVERT(VARCHAR,GETutcDATE(),114)

Result
06:21:55:597

And if you are looking for a SQL query to find the SQL version, then try this

select @@version

download

Generate fonts from SVGs using Icomon

ICOMOON , a free vector icon pack is used to generate the fonts for the icons. It is a free vector icon pack.

Steps you need to follow to generate the desired font –

1. Open https://icomoon.io/ and click IcoMoon App in the top right corner of the page to generate the fonts.

Screenshot (92) (1)

Now, you will see the untitled project with some of the default set of Icomoon icons that you may use as well, if you like.

Screenshot (93) (1)

If you do not want to use any of the icons from the default set, then you may remove the set completely by selecting the small icon on the far right and selecting Remove Set. This will keep your project clean and manageable.

Note

It is not necessary to remove the default set of icons, as the fonts will be generated only for the icons you will select.

Screenshot (94) (1)

     2. Importing Icons (SVGs)

If you have a json with all the icons that are already being used in the application, then you should import the same into icomoon app as shown below.

Click on Import Icons and select the json file as below

Screenshot (95) (1)
Untitled

Now you can see all the icons that you are using in the application. To generate font for the new icon, you need to import the SVG for that icon first as below.

Note

You can import the new icon to any of the existing sets.

Select the icon on far right and click Import to Set to select the SVG file.

Screenshot (96) (1)

After import, you can see the selected icon (SVG) as below. You can see that all the other icons are selected (with yellow border) and the one that we just imported is not selected yet.

To generate the font for the new icon, you need to select it (or select/deselect all the icons that you want to generate the fonts for)

Screenshot (97) (1)

3. Generating Fonts

To generate the fonts for the selected icons, click on Generate Font button at the bottom.

Screenshot (98) (1)

Now you could see all the icons (Glyphs) with the unicode values to be referenced in the HTML.

Screenshot (99) (1)

Hover over one of them and click Get Code to get the html to be added in the application.

Screenshot (100) (1)

       4. Download Font

The next step is to download the font. For this, you need to click on Download button at the bottom as shown below.

Screenshot22

The downloaded .zip file contains the font in various formats, a demo file you can use as a reference, a .json file that can be re-uploaded to restore your project and, a .css file encoding your icons.

Untitled (1)

Now, replace the new files with the existing ones (to include the new font we just generated). You need to replace the below files –

  • selection.json
  • all the files under fonts folder

And that’s it ! You are done ! The new icon is ready to use.