Exemple #1
0
        public string NewMediaObject(FileData mediaObject)
        {
            var mediaFile = new WorkflowMediaFile
            {
                FileName = mediaObject.name,
                Title    = mediaObject.name,
                Culture  = DataLocalizationFacade.DefaultLocalizationCulture.Name,
                Length   = mediaObject.bits.Count(),
                MimeType = MimeTypeInfo.GetCanonical(mediaObject.type)
            };

            if (mediaFile.MimeType == MimeTypeInfo.Default)
            {
                mediaFile.MimeType = MimeTypeInfo.GetCanonicalFromExtension(Path.GetExtension(mediaFile.FileName));
            }

            using (Stream readStream = new MemoryStream(mediaObject.bits))
            {
                using (Stream writeStream = mediaFile.GetNewWriteStream())
                {
                    readStream.CopyTo(writeStream);
                }
            }
            string folderPath = string.Format("/Blog/{0}/{1:yyyy-MM-dd}", Author.Name, DateTime.Now);

            mediaFile.FolderPath = ForceGetMediaFolderPath(folderPath);
            var addedFile = DataFacade.AddNew <IMediaFile>(mediaFile);

            return(MediaUrlHelper.GetUrl(addedFile));
        }
        private IMediaFile ImportMedia(string src, string folder)
        {
            if (!ImportedImages.ContainsKey("src"))
            {
                ImportedImages[src] = null;
                ForceMediaFolder(folder);

                var client = new WebClient
                {
                    Encoding = System.Text.Encoding.UTF8
                };
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

                var stream = client.OpenRead(src);

                var filename = CleanFileName(Path.GetFileName(src));
                var title    = Path.GetFileNameWithoutExtension(src);

                var mediaFile = new WorkflowMediaFile
                {
                    FileName    = Path.GetFileName(filename),
                    FolderPath  = folder,
                    Title       = title,
                    Description = string.Empty,
                    Culture     = Composite.C1Console.Users.UserSettings.ActiveLocaleCultureInfo.Name,
                    MimeType    = MimeTypeInfo.GetCanonicalFromExtension(Path.GetExtension(filename))
                };

                if (mediaFile.MimeType == MimeTypeInfo.Default)
                {
                    mediaFile.MimeType = MimeTypeInfo.GetCanonical(client.ResponseHeaders["content-type"]);
                }

                using (Stream readStream = stream)
                {
                    using (Stream writeStream = mediaFile.GetNewWriteStream())
                    {
                        readStream.CopyTo(writeStream);
                    }
                }

                int    counter   = 0;
                string extension = Path.GetExtension(mediaFile.FileName);
                string name      = mediaFile.FileName.GetNameWithoutExtension();
                while (Exists(mediaFile))
                {
                    counter++;
                    mediaFile.FileName = name + counter.ToString() + extension;
                }
                IMediaFile addedFile = DataFacade.AddNew <IMediaFile>(mediaFile);

                ImportedImages[src] = addedFile;
            }
            return(ImportedImages[src]);
        }
Exemple #3
0
        private static void Extract(string parentPath, Stream compressedStream,
                                    out IList <IMediaFileFolder> folders, out IList <IMediaFile> files)
        {
            folders = new List <IMediaFileFolder>();
            files   = new List <IMediaFile>();

            using (var zipArchive = new ZipArchive(compressedStream))
            {
                foreach (var entry in zipArchive.Entries)
                {
                    if (entry.FullName.EndsWith("/"))
                    {
                        CreateFoldersRec(folders, parentPath, entry.FullName);
                    }
                    else
                    {
                        var directory = entry.FullName.GetDirectory('/');

                        string fileName = entry.Name;

                        var mediaFile = new WorkflowMediaFile
                        {
                            FileName      = fileName,
                            Title         = fileName.GetNameWithoutExtension(),
                            FolderPath    = parentPath.Combine(directory, '/'),
                            CreationTime  = DateTime.Now,
                            Culture       = C1Console.Users.UserSettings.ActiveLocaleCultureInfo.Name,
                            LastWriteTime = DateTime.Now,
                            MimeType      = MimeTypeInfo.GetCanonicalFromExtension(Path.GetExtension(fileName))
                        };

                        int length = CopyZipData(entry.Open(), mediaFile);
                        mediaFile.Length = length;

                        files.Add(mediaFile);

                        if (directory != "")
                        {
                            CreateFoldersRec(folders, parentPath, directory);
                        }
                    }
                }
            }

            folders = folders.Distinct(new FolderComparer()).Where(x => x.Path != string.Empty).OrderBy(x => x.Path.Length).ToList();
        }
Exemple #4
0
        private bool FilePreviewable(FileInfo fileInfo)
        {
            if (string.IsNullOrEmpty(fileInfo.Extension))
            {
                return(false);
            }

            string mimeType = MimeTypeInfo.GetCanonicalFromExtension(fileInfo.Extension);

            if (string.IsNullOrEmpty(mimeType))
            {
                return(false);
            }

            return(MimeTypeInfo.IsBrowserPreviewableFile(mimeType) ||
                   (MimeTypeInfo.IsTextFile(mimeType) && fileInfo.Length < 1 << 20 /* 1 MB */));
        }
        private bool FilePreviewable(FileInfo fileInfo)
        {
            if (string.IsNullOrEmpty(fileInfo.Extension))
            {
                return(false);
            }

            string mimeType = MimeTypeInfo.GetCanonicalFromExtension(fileInfo.Extension);

            if (string.IsNullOrEmpty(mimeType))
            {
                return(false);
            }

            return(MimeTypeInfo.IsBrowserPreviewableFile(mimeType) ||
                   (MimeTypeInfo.IsTextFile(mimeType) && fileInfo.Length < 1 << 20 /* 1 MB */) ||
                   fileInfo.Extension.Equals(".dll", StringComparison.OrdinalIgnoreCase));
        }
        private static void Extract(string parentPath, Stream compressedStream,
                                    out IList <IMediaFileFolder> folders, out IList <IMediaFile> files)
        {
            folders = new List <IMediaFileFolder>();
            files   = new List <IMediaFile>();

            using (ZipInputStream zipInputStream = new ZipInputStream(compressedStream))
            {
                ZipEntry theEntry;
                while ((theEntry = zipInputStream.GetNextEntry()) != null)
                {
                    if (theEntry.IsDirectory)
                    {
                        CreateFoldersRec(folders, parentPath, theEntry.Name);
                    }
                    else
                    {
                        string directory = theEntry.Name.GetDirectory('/');

                        WorkflowMediaFile mediaFile = new WorkflowMediaFile();
                        int length = CopyZipData(zipInputStream, mediaFile);

                        mediaFile.FileName   = Path.GetFileName(theEntry.Name);
                        mediaFile.Title      = mediaFile.FileName.GetNameWithoutExtension();
                        mediaFile.FolderPath = parentPath.Combine(directory, '/');

                        mediaFile.CreationTime  = DateTime.Now;
                        mediaFile.Culture       = C1Console.Users.UserSettings.ActiveLocaleCultureInfo.Name;
                        mediaFile.LastWriteTime = DateTime.Now;
                        mediaFile.Length        = length;
                        mediaFile.MimeType      = MimeTypeInfo.GetCanonicalFromExtension(Path.GetExtension(theEntry.Name));

                        files.Add(mediaFile);

                        if (directory != "")
                        {
                            CreateFoldersRec(folders, parentPath, directory);
                        }
                    }
                }
            }

            folders = folders.Distinct(new FolderComparer()).Where(x => x.Path != string.Empty).OrderBy(x => x.Path.Length).ToList();
        }