Example #1
0
    public static string StoreMediaTemp(HttpContextBase context, HttpPostedFileBase file, UploadType type, out string thumbPath, out string physicalPath, out TimeSpan duration, out string fileType)
    {
        duration = TimeSpan.Zero;
        fileType = "";
        if (context.Session != null)
        {
            UrlFriendlyGuid GUID = (UrlFriendlyGuid) context.Session["UploadGUID"];

            List<UploadedContent> lastSavedFile = new List<UploadedContent>();

            if (context.Session["SavedFileList"] != null)
                lastSavedFile = (List<UploadedContent>) context.Session["SavedFileList"];

            physicalPath = TempPathForUpload(context, file.FileName, type, GUID);
            thumbPath = TempPathForUpload(context, file.FileName, type, GUID, true);

            //Todo: Use mimes here instead of basic extension check
            if (ImageExtensions.Contains(Path.GetExtension(file.FileName).ToLower()))
            {
                var image = (Bitmap) Image.FromStream(file.InputStream);


                var fullImage =
                    (Bitmap) ImageUtilities.Resize(image, image.Width, image.Height, RotateFlipType.RotateNoneFlipNone); //No need to resize
                ImageUtilities.SaveImage(fullImage, physicalPath, ImageFormat.Jpeg, true);



                var thumbNail =
                    (Bitmap) ImageUtilities.Resize(image, 216, 132, RotateFlipType.RotateNoneFlipNone);
                ImageUtilities.SaveImage(thumbNail, thumbPath, ImageFormat.Jpeg, true);

                duration = new TimeSpan(0,0,10);

                fileType = "Image";
            }
            else
            {
                var extension = Path.GetExtension(file.FileName);
                if (extension != null && extension.ToLower()==(".txt"))
                {
                    FileUtilities.SaveStream(file.InputStream,physicalPath,false);
                    duration = new TimeSpan(0, 0, 20);
                    var text = new StreamReader(physicalPath).ReadToEnd();
                    var ssHot = CreateImage(text.Substring(0, text.Length>15?15:text.Length));
                    thumbPath = thumbPath.Replace(Path.GetExtension(thumbPath), ".jpg");
                    ssHot.Save(thumbPath);

                    fileType = "Marquee";
                }
                else
                {
                    var s = Path.GetExtension(file.FileName);
                    if (s != null && (s.ToLower() == (".ppt") || s.ToLower() == (".pps") || s.ToLower() == (".pptx") || s.ToLower() == (".odt"))) // Powerpoint presentation
                    {
                        string path = context.Server.MapPath("~/Logs/" + "serverlog.txt");

                        Logger.WriteLine(path, "UploadRepository:  Powerpoint");

                        FileUtilities.SaveStream(file.InputStream, physicalPath, false);

                        Logger.WriteLine(path, "UploadRepository:  Saved File @ " + physicalPath);
                        var finalPath = Path.ChangeExtension(physicalPath, "wmv");
                        Microsoft.Office.Interop.PowerPoint._Presentation objPres;
                        var objApp = new Microsoft.Office.Interop.PowerPoint.Application();

                        objApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                        objApp.Activate();
                        try
                        {
                            objPres = objApp.Presentations.Open(physicalPath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse); //Last value causes powerpoint to physically open
                            // Thread.Sleep(10000);
                            objPres.SaveAs(Path.GetFullPath(finalPath), Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsWMV);
                            Logger.WriteLine(path, "UploadRepository:  SaveCopy As Successfully Started @ " + physicalPath + " to "+ finalPath);
               
                            long len = 0;
                            do
                            {
                                System.Threading.Thread.Sleep(500);
                                try
                                {
                                    FileInfo f = new FileInfo(finalPath);
                                    len = f.Length;
                                    Logger.WriteLine(path, "UploadRepository:  SaveCopy Current Length  " + len);
               
                                }
                                catch
                                {
                                    //continue;
                                }
                            }
                            while (len == 0);
                            objPres.Close();
                            objApp.Quit();

                            Marshal.ReleaseComObject(objPres);
                            Marshal.ReleaseComObject(objApp);

                            objApp = null;
                            objPres = null;

                            GC.Collect();
                            GC.WaitForPendingFinalizers();
                            Logger.WriteLine(path, "UploadRepository:  SaveCopy Done, Creating Thumbnails  ");
           

                            thumbPath = thumbPath.Replace(Path.GetExtension(thumbPath), ".jpg");
                            // thumbPath = "Content\\Images\\powerpoint.jpg";
                            duration = VideoUtilities.GetVideoDuration(finalPath);
                            // duration = new TimeSpan(0, 0, 0,60);
                            VideoUtilities.GetVideoThumbnail(finalPath, thumbPath);

                            physicalPath = finalPath;
                            fileType = "Powerpoint";
                        }
                        catch (COMException exception)
                        {
              
                            Logger.WriteLine(path, "UploadRepository: " + exception.StackTrace + "\n" + exception.Message + " Powerpoint fin:" + finalPath +" phys:" +physicalPath);

                            //   Logger.WriteLine(path, greenlotsInfo.email);

                            //    throw exception;
                        }

                    }
                    else // Must Be Video
                    {
                        FileUtilities.SaveStream(file.InputStream, physicalPath, false);

                        thumbPath= thumbPath.Replace(Path.GetExtension(thumbPath), ".jpg");
                        duration = VideoUtilities.GetVideoDuration(physicalPath);

                        VideoUtilities.GetVideoThumbnail(physicalPath,thumbPath);

                        fileType = "Video";
                    }
                }
            }

            var uploadedContent = new UploadedContent
                                      {
                                          MediaGuid = GUID,
                                          Type = type,
                                          Pictures = new List<string>(2),
                                          Duration = duration
                                      };

            if (physicalPath != null)
                uploadedContent.Pictures.Add(ResolvePath(context, physicalPath));
            if (thumbPath != null)
                uploadedContent.Pictures.Add(ResolvePath(context, thumbPath));



            if (uploadedContent.Pictures.Count > 0)
                lastSavedFile.Add(uploadedContent);


            context.Session["SavedFileList"] = lastSavedFile;


            return "Upload Sucessful";
        }
        thumbPath = "";
        physicalPath = "";

        return "Failed To Upload File(s)";
    }
Example #2
0
    public static string[] GetFileUrl(HttpContextBase context, string mediaID, string ownerID , UploadType type, out UploadedContent upload)
    {
        upload = null;
        try
        {


            if (context.Session != null)
            {
                var uploadedContents =
                    ((List<UploadedContent>) context.Session["SavedFileList"]);
                var guid = (UrlFriendlyGuid) context.Session["UploadGUID"];
                if (uploadedContents != null && uploadedContents.Count>0)
                {
                    UploadedContent thisContent = uploadedContents.Find(
                                                      content =>
                                                      content.MediaGuid == guid && content.Type == type);

                    upload = thisContent;

                    List<string> pictures = thisContent.
                                            Pictures;

                    string newPath = PathForUpload(context,
                                                   pictures[0],
                                                   ownerID,
                                                   mediaID,
                                                   guid
                                                   , type,
                                                   false);

                    string thumbPath = PathForUpload(context,
                                                     pictures[0],
                                                     ownerID,
                                                     mediaID,
                                                     guid
                                                     , type,
                                                     true);


                    FileUtilities.FolderCreate(Path.GetDirectoryName(newPath));
                    File.Copy(TempPathForUpload(context, pictures[0], type, guid),newPath);
                    try //Maybe No Thumbnail
                    {
                        if (!pictures[1].ToLower().Contains("Content\\".ToLower()))
                        {
                            File.Copy(TempPathForUpload(context, pictures[1], type, guid, true), thumbPath);
                        }
                        else
                        {
                            File.Copy(context.Server.MapPath("~/"+pictures[1]),thumbPath);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("No Thumbnail" + ex.Message);
                    }
                    uploadedContents.Remove(thisContent);

                    context.Session["SavedFileList"] = uploadedContents;

                    return new string[] { ResolvePath(context, newPath), ResolvePath(context, thumbPath) };
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);

        }
        return null;
    }