private string CopyFile(string sUrl, string sTitle, out int iFileSize)
        {
            string sContentDispositionHeader = "";
            string sContentType = "";

            byte[] fileData = null;
            iFileSize = 0;
            try
            {
                fileData = WebRequest(sUrl, out sContentDispositionHeader, out sContentType);
                if (fileData == null)
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Util.AddWarning("Error requesting file: " + sUrl + ", file is skipped. Error message: " + ex.Message);
                return(null);
            }

            try
            {
                // Use title supplied and content type instead of sContentDispositionHeader
                if (sContentDispositionHeader == null)
                {
                    if (sContentType.ToLower().IndexOf("jpeg") > -1)
                    {
                        sContentDispositionHeader = sTitle + ".jpg";
                    }
                    if (sContentType.ToLower().IndexOf("gif") > -1)
                    {
                        sContentDispositionHeader = sTitle + ".gif";
                    }
                    if (sContentType.ToLower().IndexOf("png") > -1)
                    {
                        sContentDispositionHeader = sTitle + ".png";
                    }
                    if (sContentType.ToLower().IndexOf("bmp") > -1)
                    {
                        sContentDispositionHeader = sTitle + ".bmp";
                    }
                    if (sContentType.ToLower().IndexOf("pdf") > -1)
                    {
                        sContentDispositionHeader = sTitle + ".pdf";
                    }
                    if (sContentType.ToLower().IndexOf("msword") > -1)
                    {
                        sContentDispositionHeader = sTitle + ".doc";
                    }
                }

                sContentDispositionHeader = sContentDispositionHeader.Replace("attachment; filename=", "");
                // There HAS to be an extension
                if (sContentDispositionHeader.IndexOf(".") == -1)
                {
                    return(null);
                }

                bool   bIsImage = false;
                string sExt     = Path.GetExtension(sContentDispositionHeader).ToLower();
                if (sExt == ".jpg" || sExt == ".jpeg" || sExt == ".gif" || sExt == ".png" || sExt == ".bmp" || sExt == ".tif" ||
                    sExt == ".tiff" || sExt == ".psd" || sExt == ".eps")
                {
                    bIsImage = true;
                }


                sContentDispositionHeader = System.Web.HttpUtility.UrlDecode(sContentDispositionHeader);
                GalleryAPISoapClient galleryAPI = new GalleryAPISoapClient("GalleryAPISoap", _PluginOptions[1].Value + "/GalleryAPI.asmx");

                iFileSize = fileData.Length;
                string sHash = CRC32.ComputeCRC32(fileData);

                SearchItemStruct   foundItem   = null;
                SearchItemStruct[] searchItems = null;
                if (bIsImage)
                {
                    searchItems = galleryAPI.Search("", "gallery/Private/" + _sLastDepartmentName + "/Gamle billeder/" + sContentDispositionHeader);
                }
                else
                {
                    searchItems = galleryAPI.Search("", "gallery/Private/" + _sLastDepartmentName + "/Dokumenter/" + sContentDispositionHeader);
                }
                foreach (SearchItemStruct item in searchItems)
                {
                    if (item.CRC32 == sHash)
                    {
                        foundItem = item;
                        break;
                    }
                }

                if (foundItem != null)
                {
                    return(foundItem.Path);
                }
                else
                {
                    if (bIsImage)
                    {
                        return(galleryAPI.UploadFile("/sitecore/media library/gallery/Private/" + _sLastDepartmentName + "/Gamle billeder", sContentDispositionHeader, fileData));
                    }
                    else
                    {
                        return(galleryAPI.UploadFile("/sitecore/media library/gallery/Private/" + _sLastDepartmentName + "/Dokumenter", sContentDispositionHeader, fileData));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #2
0
 // static constructor
 static CRC32()
 {
     _crc32TablesCache = Hashtable.Synchronized(new Hashtable());
     _defaultCRC       = new CRC32();
 }