Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Garrison 22 posts 31 karma points
    Feb 02, 2010 @ 22:20
    Garrison
    0

    "The directory is not empty." error in Runway Gallery

    I've installed the runway gallery and each time I attempt a bulk upload i receive the following error:

    The directory is not empty.

     

    The exception is getting thrown at line 41 of Umbraco.RunwayGallery.cs, which reads: Directory.Delete(zipDir);

    None of the images from the zip appear to be created as content, and a new, empty directory appears in Media after the error appears.

    Has anyone seen this problem before?  Is there a known fix?  My google-fu gave me this link to the old forums, but as you can see there was no response.

    Thanks,

    Garrison

     

     

  • Garrison 22 posts 31 karma points
    Feb 11, 2010 @ 03:52
    Garrison
    1

    Someone?  Anyone?  Bueller?

  • Karl Kopp 121 posts 227 karma points
    Feb 11, 2010 @ 04:55
    Karl Kopp
    0

    I would start by using Filemon (now part of Precess Monitor) and monitor the directory:

    http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx

    This will show you where and why the errors are thrown (file locked / permission error etc).

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Feb 11, 2010 @ 20:10
    Morten Christensen
    0

    Is the content of your zip actually unpacked to the media-folder? After refreshing the content tree you should be able to see your images in the tree under your album, but they won't appear in the Media section (only in the filesystem).

    Which version of Umbraco are you running? And does your Media folder have the correct permissions set?

    - Morten

  • Garrison 22 posts 31 karma points
    Feb 14, 2010 @ 21:48
    Garrison
    0

    We're running version v4.0.3.  Network Service has been granted full access to the Media folder.

    The contents of the zip dont show up in the media folder, and the zip file itself doesnt show up either. I'll check out filemon and see if that sheds any light on the issue.

    Thanks,

    -G

     

  • Peter Dijksterhuis 1442 posts 1722 karma points
    Feb 14, 2010 @ 22:56
    Peter Dijksterhuis
    0

    I think you should give Network Service full permission on the data-folder as well. If I am not mistaken, the zip gets saved there first and after all files are processed, it should be deleted there.

    HTH,

    Peter

  • Garrison 22 posts 31 karma points
    Feb 16, 2010 @ 05:37
    Garrison
    0

    Interesting.  I'll double-check permissions there as well.

    Thanks!

  • Garrison 22 posts 31 karma points
    Feb 16, 2010 @ 06:45
    Garrison
    0

    Well, sometime between when I made this post and 10 minutes ago this started working for us.  We did system maintenance over the weekend and rebooted this particular box, so that may have had an effect.  Thanks for giving us a hand!

    Garrison

  • Garrison 22 posts 31 karma points
    Feb 16, 2010 @ 18:48
    Garrison
    0

    Well, I spoke too soon.  The problem did not go away.  It seems its intermittent.

    Also, I found the cause!  It seems that the supported image types are CASE SENSITIVE.

    The ImageFileTypes in umbracoSetting.config are all lowercase strings.  If your images have extensions that are UPPER CASE, then the media elements dont get created and the directory doesnt get deleted, which throws the error.

     

    Solution:  There are several... rename your images to use lower case extensions, add upper case extensions to the  umbracoSetting.config file, or update Umbraco.RunwayGallery.cs to work with lowercase AND uppercase strings.  I'm working on the latter, and will upload my changes once I'm finished.

  • Garrison 22 posts 31 karma points
    Feb 16, 2010 @ 18:56
    Garrison
    1

    Here you go.  This seems to be working for us.

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.IO;
    using System.Collections;

    using umbraco.interfaces;
    using umbraco.cms.businesslogic.web;
    using ICSharpCode.SharpZipLib.Zip;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;

    namespace Umbraco.RunwayGallery
    {
    public class ZipUploadHandler : umbraco.BusinessLogic.ApplicationBase
    {
    public ZipUploadHandler()
    {
    umbraco.content.AfterUpdateDocumentCache += new umbraco.content.DocumentCacheEventHandler(content_AfterUpdateDocumentCache);

    }

    void content_AfterUpdateDocumentCache(Document sender, umbraco.cms.businesslogic.DocumentCacheEventArgs e)
    {
    if (sender.ContentType.Alias == "runwayGalleryAlbum")
    {
    if (sender.getProperty("zipBulkUpload") != null && !String.IsNullOrEmpty(sender.getProperty("zipBulkUpload").Value.ToString()))
    {
    string zipFile = umbraco.GlobalSettings.FullpathToRoot + sender.getProperty("zipBulkUpload").Value.ToString();

    // Loop through and extract all images
    string zipDir = unpackZip(zipFile);

    foreach (string file in Directory.GetFiles(zipDir))
    {
    createMedia(file, sender);
    }

    // Delete zipped files
    Directory.Delete(zipDir, true);

    // Remove property
    sender.getProperty("zipBulkUpload").Value = "";
    }
    }
    }

    private void createMedia(string filePath, Document parent)
    {

    // this method contains redundant code from the upload datatype as it's not possible
    // to access the methods used by the upload datatype to generate thumbnails and update
    // image sizes

    FileInfo imageFile = new FileInfo(filePath);
    if (umbraco.UmbracoSettings.ImageFileTypes.ToUpper().Contains(imageFile.Extension.ToUpper().Replace(".", "")))
    {
    Document image = Document.MakeNew(umbraco.helper.SpaceCamelCasing(imageFile.Name.Replace(imageFile.Extension, "")), DocumentType.GetByAlias("RunwayGalleryPhoto"), parent.User, parent.Id);

    // move file
    string imagePropertyId = image.getProperty("umbracoFile").Id.ToString();
    string filename = "";
    string fullFilePath = "";
    if (umbraco.UmbracoSettings.UploadAllowDirectories)
    {
    // Create a new folder in the /media folder with the name /media/propertyid
    System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../media/" + imagePropertyId));
    fullFilePath = System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../media/" + imagePropertyId + "/" + imageFile.Name);
    File.Move(filePath, fullFilePath);
    filename = "/media/" + imagePropertyId + "/" + imageFile.Name;
    image.getProperty("umbracoFile").Value = filename;
    }
    else
    {
    filename = imagePropertyId + "-" + filename;
    fullFilePath = System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../media/" + filename);
    File.Move(filePath, fullFilePath);
    image.getProperty("umbracoFile").Value = "/media/" + filename;
    }

    imageFile = new FileInfo(fullFilePath);

    // save extension
    image.getProperty("umbracoExtension").Value = imageFile.Extension.Replace(".", "");

    // save size
    image.getProperty("umbracoBytes").Value = imageFile.Length.ToString();

    // generate thumbnails
    generateThumbnails(image, imageFile);

    // publish image
    image.Publish(image.User);
    umbraco.library.PublishSingleNode(image.Id);

    }
    }

    private void generateThumbnails(Document imageDocument, FileInfo imageFile)
    {
    int fileWidth;
    int fileHeight;

    FileStream fs = new FileStream(imageFile.FullName,
    FileMode.Open, FileAccess.Read, FileShare.Read);

    System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
    fileWidth = image.Width;
    fileHeight = image.Height;
    fs.Close();
    try
    {
    imageDocument.getProperty("umbracoWidth").Value = fileWidth.ToString();
    imageDocument.getProperty("umbracoHeight").Value = fileHeight.ToString();
    }
    catch { }

    // Generate thumbnails
    string fileNameThumb = imageFile.FullName.Replace(imageFile.Extension, "_thumb");
    generateThumbnail(image, 100, fileWidth, fileHeight, imageFile.FullName, imageFile.Extension.Replace(".", ""), fileNameThumb + ".jpg");
    SortedList thumbnailSizesList = umbraco.cms.businesslogic.datatype.PreValues.GetPreValues(imageDocument.getProperty("umbracoFile").PropertyType.DataTypeDefinition.Id);
    string thumbnails = "";
    if (thumbnailSizesList.Count > 0)
    {
    string[] thumbnailSizes = thumbnailSizesList[0].ToString().Split(";".ToCharArray());
    foreach (string thumb in thumbnailSizes)
    if (thumb != "")
    generateThumbnail(image, int.Parse(thumb), fileWidth, fileHeight, imageFile.FullName, imageFile.Extension.Replace(".", ""), fileNameThumb + "_" + thumb + ".jpg");
    }

    image.Dispose();
    }


    private void generateThumbnail(System.Drawing.Image image, int maxWidthHeight, int fileWidth, int fileHeight, string fullFilePath, string ext, string thumbnailFileName)
    {
    // Generate thumbnail
    float fx = (float)fileWidth / (float)maxWidthHeight;
    float fy = (float)fileHeight / (float)maxWidthHeight;
    // must fit in thumbnail size
    float f = Math.Max(fx, fy); //if (f < 1) f = 1;
    int widthTh = (int)Math.Round((float)fileWidth / f); int heightTh = (int)Math.Round((float)fileHeight / f);

    // fixes for empty width or height
    if (widthTh == 0)
    widthTh = 1;
    if (heightTh == 0)
    heightTh = 1;

    // Create new image with best quality settings

    Bitmap bp = new Bitmap(widthTh, heightTh);
    Graphics g = Graphics.FromImage(bp);
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.PixelOffsetMode = PixelOffsetMode.HighQuality;

    // Copy the old image to the new and resized
    Rectangle rect = new Rectangle(0, 0, widthTh, heightTh);
    g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);

    // Copy metadata
    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
    ImageCodecInfo codec = null;
    for (int i = 0; i < codecs.Length; i++)
    {
    if (codecs[i].MimeType.Equals("image/jpeg"))
    codec = codecs[i];
    }

    // Set compresion ratio to 90%
    EncoderParameters ep = new EncoderParameters();
    ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);

    // Save the new image
    bp.Save(thumbnailFileName, codec, ep);
    bp.Dispose();
    g.Dispose();

    }

    private string unpackZip(string zipFile)
    {
    string tempDir = HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.StorageDirectory) + Path.DirectorySeparatorChar + Guid.NewGuid().ToString();
    Directory.CreateDirectory(tempDir);

    ZipInputStream s = new ZipInputStream(File.OpenRead(zipFile));

    ZipEntry theEntry;
    while ((theEntry = s.GetNextEntry()) != null)
    {
    string directoryName = Path.GetDirectoryName(theEntry.Name);
    string fileName = Path.GetFileName(theEntry.Name);

    if (fileName != String.Empty)
    {
    FileStream streamWriter = File.Create(tempDir + Path.DirectorySeparatorChar + fileName);

    int size = 2048;
    byte[] data = new byte[2048];
    while (true)
    {
    size = s.Read(data, 0, data.Length);
    if (size > 0)
    {
    streamWriter.Write(data, 0, size);
    }
    else
    {
    break;
    }
    }

    streamWriter.Close();

    }
    }

    // Clean up
    s.Close();
    File.Delete(zipFile);

    return tempDir;
    }
    }
    }
  • Chris Walker 1 post 21 karma points
    Nov 17, 2010 @ 02:11
    Chris Walker
    0

    Thank you! Saved me potentially hours trying to work this out.

Please Sign in or register to post replies

Write your reply to:

Draft