コード例 #1
0
        public void CheckIfFileName_IsValid(string fileName, bool answer)
        {
            // Act
            bool isValid = FileExts.IsFileNameValid(fileName);

            // Assert
            Assert.AreEqual(answer, isValid);
        }
コード例 #2
0
        public static string GetFileNameFrom(Uri uri)
        {
            string filename;

            filename = uri.LocalPath.Split('/').Last();
            filename = FileExts.ToSafeFileName(filename);
            return(filename);
        }
コード例 #3
0
        public void ConvertFileName_ToSafeFileName_ShouldPass_IsValidFileNameTest(string invalidFileName)
        {
            // Act
            string validFileName = FileExts.ToSafeFileName(invalidFileName);

            // Assert
            Assert.IsNotNull(validFileName);
            Assert.IsFalse(FileExts.IsFileNameValid(invalidFileName));
            Assert.IsTrue(FileExts.IsFileNameValid(validFileName));
        }
コード例 #4
0
        public static string DoUpload(string fileUri, HttpRequest request)
        {
            _fileName = GetCorrectName(Path.GetFileName(fileUri));

            var curExt = (Path.GetExtension(_fileName) ?? "").ToLower();

            if (!FileExts.Contains(curExt))
            {
                throw new Exception("File type is not supported");
            }

            var req = (HttpWebRequest)WebRequest.Create(fileUri);

            try
            {
                // hack. http://ubuntuforums.org/showthread.php?t=1841740
                if (IsMono)
                {
                    ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true;
                }

                using (var stream = req.GetResponse().GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("stream is null");
                    }
                    const int bufferSize = 4096;

                    using (var fs = File.Open(StoragePath(_fileName, null), FileMode.Create))
                    {
                        var buffer = new byte[bufferSize];
                        int readed;
                        while ((readed = stream.Read(buffer, 0, bufferSize)) != 0)
                        {
                            fs.Write(buffer, 0, readed);
                        }
                    }
                }

                var histDir = HistoryDir(StoragePath(_fileName, null));
                Directory.CreateDirectory(histDir);
                File.WriteAllText(Path.Combine(histDir, "createdInfo.json"), new JavaScriptSerializer().Serialize(new Dictionary <string, object> {
                    { "created", DateTime.Now.ToString() },
                    { "id", request.Cookies.GetOrDefault("uid", "uid-1") },
                    { "name", request.Cookies.GetOrDefault("uname", "John Smith") }
                }));
            }
            catch (Exception)
            {
            }
            return(_fileName);
        }
コード例 #5
0
        public static string DoUpload(string fileUri, HttpRequest request)
        {
            _fileName = GetCorrectName(Path.GetFileName(fileUri));

            var curExt = (Path.GetExtension(_fileName) ?? "").ToLower();

            if (!FileExts.Contains(curExt))
            {
                throw new Exception("File type is not supported");
            }

            var req = (HttpWebRequest)WebRequest.Create(fileUri);

            try
            {
                // hack. http://ubuntuforums.org/showthread.php?t=1841740
                if (IsMono)
                {
                    ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true;
                }

                using (var stream = req.GetResponse().GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("stream is null");
                    }
                    const int bufferSize = 4096;

                    using (var fs = File.Open(StoragePath(_fileName, null), FileMode.Create))
                    {
                        var buffer = new byte[bufferSize];
                        int readed;
                        while ((readed = stream.Read(buffer, 0, bufferSize)) != 0)
                        {
                            fs.Write(buffer, 0, readed);
                        }
                    }
                }

                DocEditor.CreateMeta(_fileName, request.Cookies.GetOrDefault("uid", "uid-1"), request.Cookies.GetOrDefault("uname", "John Smith"), null);
            }
            catch (Exception)
            {
            }
            return(_fileName);
        }
コード例 #6
0
        // uploading a file by the file url and the request
        public static string DoUpload(string fileUri, HttpRequest request)
        {
            _fileName = GetCorrectName(Path.GetFileName(fileUri));  // get the correct file name if such a name already exists

            var curExt = (Path.GetExtension(_fileName) ?? "").ToLower();

            if (!FileExts.Contains(curExt))  // check if the file extension is supported by the editor
            {
                throw new Exception("File type is not supported");
            }

            var req = (HttpWebRequest)WebRequest.Create(fileUri);

            try
            {
                VerifySSL();

                using (var stream = req.GetResponse().GetResponseStream())  // get response stream of the uploading file
                {
                    if (stream == null)
                    {
                        throw new Exception("stream is null");
                    }
                    const int bufferSize = 4096;

                    using (var fs = File.Open(StoragePath(_fileName, null), FileMode.Create))
                    {
                        var buffer = new byte[bufferSize];
                        int readed;
                        while ((readed = stream.Read(buffer, 0, bufferSize)) != 0)
                        {
                            fs.Write(buffer, 0, readed);  // write bytes to the output stream
                        }
                    }
                }

                // get file meta information or create the default one
                var id   = request.Cookies.GetOrDefault("uid", null);
                var user = Users.getUser(id);  // get the user
                DocEditor.CreateMeta(_fileName, user.id, user.name, null);
            }
            catch (Exception)
            {
            }
            return(_fileName);
        }
コード例 #7
0
        public static string DoUpload(HttpContext context)
        {
            var httpPostedFile = context.Request.Files[0];

            if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
            {
                var files = httpPostedFile.FileName.Split(new char[] { '\\' });
                _fileName = files[files.Length - 1];
            }
            else
            {
                _fileName = httpPostedFile.FileName;
            }

            var curSize = httpPostedFile.ContentLength;

            if (MaxFileSize < curSize || curSize <= 0)
            {
                throw new Exception("File size is incorrect");
            }

            var curExt = (Path.GetExtension(_fileName) ?? "").ToLower();

            if (!FileExts.Contains(curExt))
            {
                throw new Exception("File type is not supported");
            }

            _fileName = GetCorrectName(_fileName);

            var savedFileName = StoragePath(_fileName, null);

            httpPostedFile.SaveAs(savedFileName);

            var histDir = HistoryDir(savedFileName);

            Directory.CreateDirectory(histDir);
            File.WriteAllText(Path.Combine(histDir, "createdInfo.json"), new JavaScriptSerializer().Serialize(new Dictionary <string, object> {
                { "created", DateTime.Now.ToString() },
                { "id", context.Request.Cookies.GetOrDefault("uid", "uid-1") },
                { "name", context.Request.Cookies.GetOrDefault("uname", "John Smith") }
            }));

            return(_fileName);
        }
コード例 #8
0
        // uploading a file by the HtthContext object
        public static string DoUpload(HttpContext context)
        {
            var httpPostedFile = context.Request.Files[0];

            if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")  // check from which browser the request came for
            {
                var files = httpPostedFile.FileName.Split(new char[] { '\\' });
                _fileName = files[files.Length - 1];  // get file name
            }
            else
            {
                _fileName = httpPostedFile.FileName;
            }

            var curSize = httpPostedFile.ContentLength;

            if (MaxFileSize < curSize || curSize <= 0)  // check if the file size exceeds the maximum file size
            {
                throw new Exception("File size is incorrect");
            }

            var curExt = (Path.GetExtension(_fileName) ?? "").ToLower();

            if (!FileExts.Contains(curExt))  // check if the file extension is supported by the editor
            {
                throw new Exception("File type is not supported");
            }

            _fileName = GetCorrectName(_fileName);            // get the correct file name if such a name already exists

            var savedFileName = StoragePath(_fileName, null); // get the storage path to the uploading file

            httpPostedFile.SaveAs(savedFileName);             // and save it

            // get file meta information or create the default one
            var id   = context.Request.Cookies.GetOrDefault("uid", null);
            var user = Users.getUser(id);  // get the user

            DocEditor.CreateMeta(_fileName, user.id, user.name, null);

            return(_fileName);
        }
コード例 #9
0
        public static string DoUpload(string fileUri)
        {
            _fileName = GetCorrectName(Path.GetFileName(fileUri));

            var curExt = (Path.GetExtension(_fileName) ?? "").ToLower();

            if (!FileExts.Contains(curExt))
            {
                throw new Exception("File type is not supported");
            }

            var req = (HttpWebRequest)WebRequest.Create(fileUri);

            try
            {
                using (var stream = req.GetResponse().GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("stream is null");
                    }
                    const int bufferSize = 4096;

                    using (var fs = File.Open(StoragePath + _fileName, FileMode.Create))
                    {
                        var buffer = new byte[bufferSize];
                        int readed;
                        while ((readed = stream.Read(buffer, 0, bufferSize)) != 0)
                        {
                            fs.Write(buffer, 0, readed);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            return(_fileName);
        }
コード例 #10
0
        public static string DoUpload(HttpContext context)
        {
            var httpPostedFile = context.Request.Files[0];

            if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
            {
                var files = httpPostedFile.FileName.Split(new char[] { '\\' });
                _fileName = files[files.Length - 1];
            }
            else
            {
                _fileName = httpPostedFile.FileName;
            }

            var curSize = httpPostedFile.ContentLength;

            if (MaxFileSize < curSize || curSize <= 0)
            {
                throw new Exception("File size is incorrect");
            }

            var curExt = (Path.GetExtension(_fileName) ?? "").ToLower();

            if (!FileExts.Contains(curExt))
            {
                throw new Exception("File type is not supported");
            }

            _fileName = GetCorrectName(_fileName);

            var savedFileName = StoragePath(_fileName, null);

            httpPostedFile.SaveAs(savedFileName);

            DocEditor.CreateMeta(_fileName, context.Request.Cookies.GetOrDefault("uid", "uid-1"), context.Request.Cookies.GetOrDefault("uname", "John Smith"), null);

            return(_fileName);
        }