示例#1
0
 public static void WriteTextFile(string path, NetworkStream stream)
 {
     try
     {
         if (File.Exists(path))
         {
             Write(stream, File.ReadAllText(path), mime: MimeType.Get(path));
         }
         else
         {
             Write(stream, $"The file '{path}' not exists", Status.NotFound);
         }
     }
     catch
     {
         Write(stream, $"Failed to read '{path}'", Status.NotFound);
     }
 }
示例#2
0
        /// <summary>
        /// Saves the edit model.
        /// </summary>
        public bool SaveAll(bool draft)
        {
            var context = HttpContext.Current;
            var hasfile = UploadedFile != null || ServerFile != null;

            byte[]    data = null;
            WebClient web  = new WebClient();

            // Check if the original URL has been updated, and if so
            if (!Content.IsNew && !String.IsNullOrEmpty(Content.OriginalUrl))
            {
                var old = Content.GetSingle(Content.Id);
                if (old != null)
                {
                    if (Content.OriginalUrl != old.OriginalUrl)
                    {
                        FileUrl = Content.OriginalUrl;
                    }
                }
            }

            // Download file from web
            if (!hasfile && !String.IsNullOrEmpty(FileUrl))
            {
                data = web.DownloadData(FileUrl);
                Content.OriginalUrl = FileUrl;
                Content.LastSynced  = Convert.ToDateTime(web.ResponseHeaders[HttpResponseHeader.LastModified]);
            }

            var media = new MediaFileContent();

            if (hasfile)
            {
                if (UploadedFile != null)
                {
                    media.Filename    = UploadedFile.FileName;
                    media.ContentType = UploadedFile.ContentType;
                    using (var reader = new BinaryReader(UploadedFile.InputStream)) {
                        media.Body = reader.ReadBytes(Convert.ToInt32(UploadedFile.InputStream.Length));
                    }
                }
                else
                {
                    media.Filename    = ServerFile.Name;
                    media.ContentType = MimeType.Get(ServerFile.Name);
                    using (var stream = ServerFile.OpenRead()) {
                        media.Body = new byte[ServerFile.Length];
                        stream.Read(media.Body, 0, media.Body.Length);
                    }
                }
            }
            else if (data != null)
            {
                media.Filename    = FileUrl.Substring(FileUrl.LastIndexOf('/') + 1);
                media.ContentType = web.ResponseHeaders["Content-Type"];
                media.Body        = data;
            }
            else
            {
                media = null;
            }

            var saved = false;

            if (!Content.IsFolder)
            {
                // Only save permalinks for non-folders
                var filename = !String.IsNullOrEmpty(Content.Filename) ? Content.Filename : (!String.IsNullOrEmpty(media.Filename) ? media.Filename : "");
                if (Permalink.IsNew && String.IsNullOrEmpty(Permalink.Name))
                {
                    Permalink.Name = Permalink.Generate(!Content.IsFolder ? filename : Content.Name, Models.Permalink.PermalinkType.MEDIA);
                }
                try {
                    Permalink.Save();
                } catch (DuplicatePermalinkException) {
                    if (Permalink.IsNew)
                    {
                        Permalink.Name = Content.Id + Permalink.Name.Substring(Permalink.Name.LastIndexOf('.'));
                        Permalink.Save();
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            else
            {
                Content.PermalinkId = Guid.Empty;
            }

            if (draft)
            {
                saved = Content.Save(media);
            }
            else
            {
                saved = Content.SaveAndPublish(media);
            }

            if (saved)
            {
                // Save related information
                Relation.DeleteByDataId(Content.Id);
                List <Relation> relations = new List <Relation>();
                ContentCategories.ForEach(c => relations.Add(new Relation()
                {
                    DataId = Content.Id, RelatedId = c, IsDraft = false, Type = Relation.RelationType.CONTENTCATEGORY
                })
                                          );
                relations.ForEach(r => r.Save());

                // Save extensions
                foreach (var ext in Extensions)
                {
                    // Call OnSave
                    ext.Body.OnManagerSave(Content);

                    ext.ParentId = Content.Id;
                    ext.Save();
                    if (!draft)
                    {
                        if (Extension.GetScalar("SELECT COUNT(extension_id) FROM extension WHERE extension_id=@0 AND extension_draft=0", ext.Id) == 0)
                        {
                            ext.IsNew = true;
                        }
                        ext.IsDraft = false;
                        ext.Save();
                    }
                }
                // Reset file url
                FileUrl = "";

                return(true);
            }
            return(false);
        }
示例#3
0
        // 패턴 체크 실행
        bool checkUrlType(HostCheck patternCheck, Session oSession)
        {
            if (oSession.HTTPMethodIs("CONNECT"))
            {
                oSession.hostname = patternCheck.afterHost();
            }
            else
            {
                if (patternCheck.isStatus())
                {
                    int status = patternCheck.getStatusCode();

                    sendResponse(oSession, status, "text/html", new byte[0]);

                    return(false);
                }

                if (patternCheck.isFolder() || patternCheck.isFile())
                {
                    string url = oSession.fullUrl;
                    int    idx = url.LastIndexOf(patternCheck.Before);

                    FileInfo file;
                    string   target;

                    if (patternCheck.isFolder())
                    {
                        string first  = url.Substring(0, idx);
                        string second = patternCheck.Before;
                        string last   = url.Replace(first, "").Replace(second, "");

                        log(first + " : " + second + " : " + last);

                        // 기본 디렉토리 지정
                        if (string.IsNullOrEmpty(last) || last.Equals("/"))
                        {
                            last = "/index.html";
                        }

                        if (last[0] != '/')
                        {
                            last = "/" + last;
                        }

                        target = patternCheck.After + last;
                    }
                    else
                    {
                        target = patternCheck.After;
                    }

                    file = new FileInfo(target);

                    if (file.Exists)
                    {
                        string content_type = MimeType.Get(file.Extension);
                        byte[] data         = File.ReadAllBytes(file.FullName);

                        sendResponse(oSession, 200, content_type, data);

                        return(false);
                    }
                }
                else
                {
                    oSession.fullUrl = patternCheck.afterUrl(oSession.fullUrl);
                }
            }

            return(true);
        }
示例#4
0
 public void Check_empty(string compare)
 {
     Assert.Equal(string.Empty, MimeType.Get(compare));
 }
示例#5
0
 public void Check_null()
 {
     Assert.Throws <ArgumentNullException>(() => MimeType.Get(null));
 }
示例#6
0
 public void Check_file_dot_svg(string reference, string compare)
 {
     Assert.Equal(reference, MimeType.Get(compare));
 }
示例#7
0
 public void Check_pdf(string reference, string compare)
 {
     Assert.Equal(reference, MimeType.Get(compare));
 }
示例#8
0
        public void GettingTypeOfKnownExtension()
        {
            var result = MimeType.Get(".json");

            result.Should().Be("application/json");
        }
示例#9
0
        public void GettingTypeOfUnknownExtension()
        {
            var result = MimeType.Get(".unk");

            result.Should().Be("application/octet-stream");
        }
示例#10
0
        public void GettingTypeOfKnownFileName()
        {
            var result = MimeType.Get("data.json");

            result.Should().Be("application/json");
        }