Esempio n. 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;
        }