Exemple #1
0
        public void CoreOnDerive(ObjectOnDerive method)
        {
            this.Revision = Guid.NewGuid();

            if (this.ExistInData || this.ExistInDataUri)
            {
                if (!this.ExistMediaContent)
                {
                    this.MediaContent = new MediaContentBuilder(this.Strategy.Session).Build();
                }
            }

            if (this.ExistInData)
            {
                this.MediaContent.Data = this.InData;
                this.MediaContent.Type = this.InType ?? MediaContents.Sniff(this.InData, this.InFileName);

                this.RemoveInType();
                this.RemoveInData();
            }

            if (this.ExistInFileName)
            {
                this.Name = Path.GetFileNameWithoutExtension(this.InFileName);
                this.RemoveInFileName();
            }

            if (this.ExistInDataUri)
            {
                var dataUrl = new DataUrl(this.InDataUri);

                this.MediaContent.Data = Convert.FromBase64String(dataUrl.ReadAsBase64EncodedString());
                this.MediaContent.Type = dataUrl.ContentType;

                this.RemoveInDataUri();
            }

            this.Type = this.MediaContent?.Type;

            var name         = !string.IsNullOrWhiteSpace(this.Name) ? this.Name : this.UniqueId.ToString();
            var fileName     = $"{name}.{MimeTypesMap.GetExtension(this.Type)}";
            var safeFileName = new string(fileName.Where(ch => !InvalidFileNameChars.Contains(ch)).ToArray());

            var uppercaseSafeFileName = safeFileName.ToUpperInvariant();

            foreach (var invalidFileName in InvalidFileNames)
            {
                if (uppercaseSafeFileName.StartsWith(invalidFileName))
                {
                    safeFileName += "_" + safeFileName;
                    break;
                }
            }

            this.FileName = safeFileName;
        }
        public void BaseOnDerive(ObjectOnDerive method)
        {
            this.Revision = Guid.NewGuid();

            if (this.ExistInData || this.ExistInDataUri)
            {
                if (!this.ExistMediaContent)
                {
                    this.MediaContent = new MediaContentBuilder(this.Strategy.Session).Build();
                }
            }

            if (this.ExistInData)
            {
                this.MediaContent.Data = this.InData;
                this.MediaContent.Type = MediaContents.Sniff(this.InData);

                this.RemoveInData();
            }

            if (this.ExistInDataUri)
            {
                var regex = new Regex(@"data:(?<mime>[\w/\-\.]+);(?<encoding>\w+),(?<data>.*)", RegexOptions.Compiled);

                var match = regex.Match(this.InDataUri);

                var mime     = match.Groups["mime"].Value;
                var encoding = match.Groups["encoding"].Value;
                var data     = match.Groups["data"].Value;

                var binaryData = Convert.FromBase64String(data);

                this.MediaContent.Data = binaryData;
                this.MediaContent.Type = mime;

                this.RemoveInDataUri();
            }
        }