public void GetSQLStatementsTest()
        {
            var databaseMock = new Mock <IDatabase>();
            var fileMock     = new Mock <IFileSystemAccess>();

            fileMock.Setup(f => f.FileExists(It.IsAny <string>())).Returns(true);
            var sqlStatements = new string[]
            {
                "SET search_path = public;",
                "SELECT * FROM \"Test\";",
                "CREATE FUNCTION test()",
                "$$;",
                "CREATE FUNCTION test()",
                "$_$;",
                "CREATE FUNCTION test()",
                "OWNER TO public;",
                "BEGIN;",
                "COMMIT;",
                "Blub",
            };

            fileMock.Setup(f => f.ReadAllLines(It.IsAny <string>())).Returns(sqlStatements);

            var file = new SQLFile(@"D:\SVN_Arbeitskopien\Unicorn\trunk\BDE\src\Unicorn.BDE.Database\0162.a.undoDiff.sql", databaseMock.Object, fileMock.Object);

            file.SQLStatements.Count().Should().Be(7);
        }
Ejemplo n.º 2
0
        public async Task <FileResult> Get(string filename)
        {
            SQLFile myFile = await _db.SQLFile.SingleAsync(p => p.FileName == filename);

            FileResult mystuff = File((await(_db.SQLFileContent.SingleAsync(m => m.SQLFileId == myFile.FileId))).Content,
                                      System.Net.Mime.MediaTypeNames.Application.Octet,
                                      myFile.FileName);

            return(mystuff);
        }
        public async Task <IActionResult> Edit(SQLFile sQLFile)
        {
            if (ModelState.IsValid)
            {
                _sqlcontext.Update(sQLFile);
                await _sqlcontext.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(sQLFile));
        }
        public async Task <IActionResult> DeleteConfirmed(long id)
        {
            SQLFile sQLFile = await _sqlcontext.SQLFile.SingleAsync(m => m.FileId == id);

            SQLFileContent sQLCont = await _sqlcontext.SQLFileContent.SingleAsync(m => m.SQLFileId == sQLFile.FileId);

            _sqlcontext.SQLFileContent.Remove(sQLCont);
            _sqlcontext.SQLFile.Remove(sQLFile);
            await _sqlcontext.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
        // GET: SQLFiles/Edit/5
        public async Task <IActionResult> Edit(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            SQLFile sQLFile = await _sqlcontext.SQLFile.SingleAsync(m => m.FileId == id);

            if (sQLFile == null)
            {
                return(NotFound());
            }
            return(View(sQLFile));
        }
        public void MarkAsExecutedTest()
        {
            var databaseMock = new Mock <IDatabase>();
            var fileMock     = new Mock <IFileSystemAccess>();

            fileMock.Setup(f => f.FileExists(It.IsAny <string>())).Returns(true);

            var file = new SQLFile("\\0001.diff.sql", databaseMock.Object, fileMock.Object);

            databaseMock.Setup(c => c.ExecuteCommandNonQuery(SQLTemplates.AddExecutedFileSql(new DatabaseVersion("\\Test\\0001" + SQLTemplates.DiffFile), FileType.Diff, "Exported from this database"))).Verifiable();

            file.MarkAsExecuted();

            databaseMock.VerifyAll();
        }
        public void ExecuteTest()
        {
            var databaseMock = new Mock <IDatabase>();
            var fileMock     = new Mock <IFileSystemAccess>();

            fileMock.Setup(f => f.FileExists(It.IsAny <string>())).Returns(true);

            var sqlStatements = new string[] { "SELECT * FROM \"Test\";" };

            fileMock.Setup(f => f.ReadAllLines(It.IsAny <string>())).Returns(sqlStatements);

            var file = new SQLFile(@"D:\SVN_Arbeitskopien\Unicorn\trunk\BDE\src\Unicorn.BDE.Database\0162.a.undoDiff.sql", databaseMock.Object, fileMock.Object);

            file.ExecuteInTransaction();
        }
Ejemplo n.º 8
0
        public async Task Remove(IList <IFormFile> UploadFiles)
        {
            IFormFile file     = UploadFiles[0];
            string    filename = ContentDispositionHeaderValue
                                 .Parse(file.ContentDisposition)
                                 .FileName
                                 .Trim('"');

            string nameonly = Path.GetFileNameWithoutExtension(filename);
            string extonly  = Path.GetExtension(filename);
            string fname    = nameonly + extonly;

            SQLFile x = await _db.SQLFile.SingleOrDefaultAsync(p => p.FileName == fname);

            if (x == null)
            {
                return;
            }

            SQLFileContent y = await _db.SQLFileContent.SingleOrDefaultAsync(p => p.SQLFileId == x.FileId);

            _db.SQLFileContent.Remove(y);
            await _db.SaveChangesAsync();

            _db.SQLFile.Remove(x);
            await _db.SaveChangesAsync();

            Response.Clear();
            Response.StatusCode = 200;
            Response.HttpContext.Features.Get <IHttpResponseFeature>().ReasonPhrase = "File removed successfully";


            //try
            //{
            //    var filename = hostingEnv.ContentRootPath + $@"\{UploadFiles[0].FileName}";
            //    if (System.IO.File.Exists(filename))
            //    {
            //        System.IO.File.Delete(filename);
            //    }
            //}
            //catch (Exception e)
            //{
            //    Response.Clear();
            //    Response.StatusCode = 200;
            //    Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File removed successfully";
            //    Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
            //}
        }
        public async Task <FileResult> GetByName(string id)
        {
            if (id == null)
            {
                //return HttpNotFound();
            }
            SQLFile sQLFile = await _sqlcontext.SQLFile.SingleAsync(m => m.FileName == id);

            if (sQLFile == null)
            {
                //return HttpNotFound();
            }


            return(File((await(_sqlcontext.SQLFileContent.SingleAsync(m => m.SQLFileId == sQLFile.FileId))).Content,
                        System.Net.Mime.MediaTypeNames.Application.Octet,
                        sQLFile.FileName));
        }
        // GET: SQLFiles/Details/5
        public async Task <IActionResult> Details(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            SQLFile sQLFile = await _sqlcontext.SQLFile.SingleAsync(m => m.FileId == id);

            if (sQLFile == null)
            {
                return(NotFound());
            }

            string fileUrl1 = Globals.ProductionUrl + "File/GetById/" + sQLFile.FileId;

            ViewBag.Url1 = fileUrl1;
            string fileUrl2 = Globals.ProductionUrl + "File/GetByName/" + sQLFile.FileName;

            ViewBag.Url2 = fileUrl2;

            return(View(sQLFile));
        }
        public void ConstructorTest()
        {
            var databaseMock = new Mock <IDatabase>();
            var fileMock     = new Mock <IFileSystemAccess>();

            new Action(() => { new SQLFile(string.Empty, databaseMock.Object, fileMock.Object); }).Should().Throw <FileNotFoundException>();

            fileMock.Setup(f => f.FileExists(It.IsAny <string>())).Returns(true);
            new Action(() => { new SQLFile(@"D:\SVN_Arbeitskopien\Unicorn\trunk\BDE\src\Unicorn.BDE.Database\0162.a.undoDiff.dat", databaseMock.Object, fileMock.Object); }).Should().Throw <ArgumentException>();

            new Action(() => { new SQLFile(@"D:\SVN_Arbeitskopien\Unicorn\trunk\BDE\src\Unicorn.BDE.Database\0162.a.undoDiff.dat", databaseMock.Object, null); }).Should().Throw <ArgumentNullException>();
            new Action(() => { new SQLFile(@"D:\SVN_Arbeitskopien\Unicorn\trunk\BDE\src\Unicorn.BDE.Database\0162.a.undoDiff.dat", null, fileMock.Object); }).Should().Throw <ArgumentNullException>();

            var file = new SQLFile(@"D:\SVN_Arbeitskopien\Unicorn\trunk\BDE\src\Unicorn.BDE.Database\0162.a.undoDiff.sql", databaseMock.Object, fileMock.Object);

            file.FileType.Should().Be(FileType.UndoDiff);

            file = new SQLFile(@"D:\SVN_Arbeitskopien\Unicorn\trunk\BDE\src\Unicorn.BDE.Database\0162.a.diff.sql", databaseMock.Object, fileMock.Object);
            file.FileType.Should().Be(FileType.Diff);

            file = new SQLFile(@"D:\SVN_Arbeitskopien\Unicorn\trunk\BDE\src\Unicorn.BDE.Database\0077.dump.sql", databaseMock.Object, fileMock.Object);
            file.FileType.Should().Be(FileType.Dump);
        }
Ejemplo n.º 12
0
        public async Task <FileResult> GetByName(string id)
        {
            if (id == null)
            {
                return(FileNotFound());
            }

            try
            {
                SQLFile sQlFile = await _sqlcontext.SQLFile.SingleAsync(m => m.FileName == id);

                if (sQlFile == null)
                {
                    return(FileNotFound());
                }

                return(File((await(_sqlcontext.SQLFileContent.SingleAsync(m => m.SQLFileId == sQlFile.FileId))).Content,
                            sQlFile.ContentType,
                            sQlFile.FileName));
            }
            catch
            { return(FileNotFound()); }
        }
        public async Task <IActionResult> Create(SQLFile sQLFile, IFormFile file)
        {
            long retId = 0;

            string fname = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.ToString().Trim('"');

            string nameonly = Path.GetFileNameWithoutExtension(fname);
            string extonly  = Path.GetExtension(fname);


            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
#pragma warning disable 162
            if (true || ModelState.IsValid)
#pragma warning restore 162
            {
                try
                {
                    if (file.Length > 0 && file.Length < 0x7FFFFFFF)
                    {
                        if (!file.ContentType.StartsWith("image") && !User.IsInRole("Admin"))
                        {
                            return(RedirectToAction("Index"));
                        }

                        SQLFileContent content = new SQLFileContent {
                            Content = new byte[file.Length]
                        };
                        using (Stream reader = file.OpenReadStream())
                        {
                            int x = await reader.ReadAsync(content.Content, 0, (int)file.Length);

                            if (x != file.Length)
                            {
                                return(RedirectToAction("Index"));
                            }
                        }

                        sQLFile.ContentType = file.ContentType;
                        sQLFile.Contributor = User.Identity.Name;
                        sQLFile.FileName    = fname;

                        _sqlcontext.SQLFile.Add(sQLFile);
                        await _sqlcontext.SaveChangesAsync();

                        retId = sQLFile.FileId;

                        content.SQLFileId = retId;
                        _sqlcontext.SQLFileContent.Add(content);
                        await _sqlcontext.SaveChangesAsync();

                        retId = content.SQLFileId;

                        fname            = nameonly + "." + retId + extonly;
                        sQLFile.FileName = fname;
                        _sqlcontext.Entry(sQLFile).State = EntityState.Modified;
                        await _sqlcontext.SaveChangesAsync();
                    }
                    else
                    {
                        return(RedirectToAction("Index"));
                    }
                }
                catch (Exception)
                {
                    //string meg = ex.Message;
                    //Log the error (uncomment dex variable name and add a line here to write a log.
                    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
                }

                if (retId < 1)
                {
                    return(RedirectToAction("Index"));
                }



                return(RedirectToAction("Index"));
            }

            // ReSharper disable once HeuristicUnreachableCode
#pragma warning disable 162
            return(RedirectToAction("Index"));

#pragma warning restore 162
        }
Ejemplo n.º 14
0
        public async Task Save(IList <IFormFile> UploadFiles)
        {
            try
            {
                foreach (IFormFile file in UploadFiles)
                {
                    var filename = ContentDispositionHeaderValue
                                   .Parse(file.ContentDisposition)
                                   .FileName
                                   .Trim('"');

                    string nameonly = Path.GetFileNameWithoutExtension(filename);
                    string extonly  = Path.GetExtension(filename);
                    string fname    = nameonly + extonly;

                    SQLFile x = await _db.SQLFile.SingleOrDefaultAsync(p => p.FileName == fname);

                    if (x != null) // already exists
                    {
                        Response.Clear();
                        Response.StatusCode = 409;
                        Response.HttpContext.Features.Get <IHttpResponseFeature>().ReasonPhrase = "File already exists";
                        return;
                    }

                    SQLFile        SqlFile        = new SQLFile();
                    SQLFileContent SqlFileContent = new SQLFileContent {
                        Content = new byte[file.Length]
                    };
                    using (Stream reader = file.OpenReadStream())
                    {
                        int xx = await reader.ReadAsync(SqlFileContent.Content, 0, (int)file.Length);

                        if (xx != file.Length)
                        {
                            Response.Clear();
                            Response.StatusCode = 204;
                            Response.HttpContext.Features.Get <IHttpResponseFeature>().ReasonPhrase = "File failed to upload";
                            return;
                        }
                    }

                    SqlFile.ContentType = file.ContentType;
                    SqlFile.Contributor = "unknown";
                    SqlFile.FileName    = filename;

                    _db.SQLFile.Add(SqlFile);
                    await _db.SaveChangesAsync();

                    long retId = SqlFile.FileId;

                    SqlFileContent.SQLFileId = retId;
                    _db.SQLFileContent.Add(SqlFileContent);
                    await _db.SaveChangesAsync();

                    retId = SqlFileContent.SQLFileId;

                    SqlFile.FileName         = fname;
                    _db.Entry(SqlFile).State = EntityState.Modified;
                    await _db.SaveChangesAsync();


                    //filename = hostingEnv.ContentRootPath + "\\wwwroot" + $@"\{filename}";
                    //if (!System.IO.File.Exists(filename))
                    //{
                    //    using (FileStream fs = System.IO.File.Create(filename))
                    //    {
                    //        file.CopyTo(fs);
                    //        fs.Flush();
                    //    }
                    //}
                }
            }
            catch (Exception e)
            {
                Response.Clear();
                Response.StatusCode = 204;
                Response.HttpContext.Features.Get <IHttpResponseFeature>().ReasonPhrase = "File failed to upload";
                Response.HttpContext.Features.Get <IHttpResponseFeature>().ReasonPhrase = e.Message;
            }
        }