Example #1
0
        public ActionResult UploadPrototype([FromForm] CreatePrototypeRequest req)
        {
            Project proj = _context.Project.Where(p => p.Uid == req.ProjectUid).FirstOrDefault <Project>();

            string         path      = Path.Combine(_env.WebRootPath, "files");
            IList <string> filePaths = new List <string>();

            if (req.Files != null)
            {
                foreach (IFormFile file in req.Files)
                {
                    string     fileName   = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
                    string     filepath   = Path.Combine(path, fileName);
                    FileStream fileStream = new FileStream(filepath, FileMode.Create);
                    file.CopyTo(fileStream);
                    fileStream.Close();
                    AzureFileService fileService = new AzureFileService(this._appSettings);
                    fileService.storeFile("files", fileName, filepath);

                    filePaths.Add(fileName);
                }
            }
            Project   Project   = _context.Project.Where(project => project.Uid == req.ProjectId).First <Project>();
            Prototype prototype = new Prototype();

            prototype.ProjectId            = Project.Id;
            prototype.PrototypeName        = req.PrototypeName;
            prototype.PrototypeDescription = req.PrototypeDescription;
            prototype.Uid           = Guid.NewGuid().ToString();
            prototype.PrototypePath = Newtonsoft.Json.JsonConvert.SerializeObject(filePaths);
            var result = _context.Prototype.Add(prototype);

            _context.SaveChanges();
            return(Ok(prototype));
        }
        public ActionResult StoreFile(CreateResearchFileRequest req)
        {
            string path = Path.Combine(_env.WebRootPath, "files");
            IList <ResearchFile> researchFiles = new List <ResearchFile>();
            Project project = _context.Project.Where(proj => proj.Uid == req.uid).First <Project>();

            if (req.Files != null)
            {
                foreach (IFormFile file in req.Files)
                {
                    string     fileName   = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
                    string     filepath   = Path.Combine(path, fileName);
                    FileStream fileStream = new FileStream(filepath, FileMode.Create);
                    file.CopyTo(fileStream);
                    fileStream.Close();
                    AzureFileService fileService = new AzureFileService(this._appSettings);
                    fileService.storeFile("files", fileName, filepath);
                    ResearchFile researchFile = new ResearchFile();
                    researchFile.FileName  = fileName;
                    researchFile.ProjectId = project.Id;
                    researchFiles.Add(researchFile);
                    _context.ResearchFile.Add(researchFile);
                }
            }

            _context.SaveChanges();
            return(Ok(researchFiles));
        }
Example #3
0
        public async Task PutRangeAsync()
        {
            const string         testContent     = "Content as String";
            IEnumerable <string> headerValues    = new List <string>();
            IEnumerable <string> xmsheaderValues = new List <string>();
            long?contentLengthReq = null;
            Action <HttpResponseMessage> respAction = (r) =>
            {
                r.Headers.TryGetValues("Authorization", out headerValues);
                r.Headers.TryGetValues("x-ms-range", out xmsheaderValues);
                contentLengthReq = r.Content.Headers.ContentLength;
            };

            var repo = new FakeHttpClientRepo(respAction);
            //var repo = new HttpClientRepo();

            var azureFileService = new AzureFileService(accountName, accessKey, repo);

            byte[] bytes = Encoding.UTF8.GetBytes(testContent);

            await azureFileService.PutRangeAsync(
                new Uri(uripath + "?comp=range"),
                bytes);

            var xmsStringComparison = $"bytes=0-{(bytes.Length - 1).ToString()}";

            Assert.IsTrue(
                bytes.Length == contentLengthReq &&
                xmsheaderValues.Any(hv => hv.Contains(xmsStringComparison)) &&
                headerValues.Any(v => v.Contains($"SharedKey {accountName}:")));
        }
Example #4
0
        public ActionResult getFile(String FileName)
        {
            AzureFileService fileService = new AzureFileService(this._appSettings);
            CloudFile        cloudFile   = fileService.getFIle("files", FileName);
            string           path        = Path.Combine(_env.WebRootPath, "files");

            cloudFile.DownloadToFile(Path.Combine(path, FileName), FileMode.Create);
            var    stream       = new FileStream(Path.Combine(path, FileName), FileMode.Open);
            var    mimeProvider = new FileExtensionContentTypeProvider();
            string mimeType;

            mimeProvider.TryGetContentType(Path.Combine(path, FileName), out mimeType);
            return(File(stream, mimeType));
        }
Example #5
0
        /// <summary>
        ///     Azure storage client builder
        /// </summary>
        /// <param name="storageSettings">Storage settings</param>
        /// <returns>AzureStorageClient</returns>
        public static AzureStorageClient Build(IStorageSettings storageSettings)
        {
            var account           = CloudStorageAccount.Parse(storageSettings.ConnectionString);
            var storageFileClient = account.CreateCloudFileClient();

            var azureFileService           = new AzureFileService();
            var azureDirectoryService      = new AzureDirectoryService(storageFileClient);
            var storageClientAzureProvider =
                new StorageClientProviderAzure(azureFileService, azureDirectoryService, storageSettings);

            var directoryService = new DirectoryService(storageClientAzureProvider);
            var fileService      = new FileService(storageClientAzureProvider);

            return(new AzureStorageClient(storageClientAzureProvider, directoryService, fileService));
        }
        public ActionResult DeleteFile(DeleteFileRequest req)
        {
            AzureFileService fileService = new AzureFileService(this._appSettings);
            var result = fileService.DeleteFile("files", req.FileName);

            if (result)
            {
                ResearchFile research = _context.ResearchFile.Where(
                    researchFile => researchFile.ProjectId == req.ProjectId &&
                    researchFile.FileName == req.FileName).FirstOrDefault();
                _context.ResearchFile.Remove(research);

                IList <ResearchFile> researchFileList = _context.ResearchFile.Where(
                    researchFile => researchFile.ProjectId == req.ProjectId).ToList();
                return(Ok(researchFileList));
            }
            return(NotFound());
        }
Example #7
0
        public async Task CreateFileAsync()
        {
            IEnumerable <string> headerValues = new List <string>();

            Action <HttpResponseMessage> respAction = (r) =>
            {
                // check whatever header or content values indicate the success of this test here
                r.Headers.TryGetValues("Authorization", out headerValues);
            };

            var repo = new FakeHttpClientRepo(respAction);
            //var repo = new HttpClientRepo();

            var azureFileService = new AzureFileService(accountName, accessKey, repo);

            await azureFileService.CreateFileAsync(new Uri(uripath));

            // Check if encryption is possible with accessKey
            Assert.IsTrue(headerValues.Any(v => v.Contains($"SharedKey {accountName}:")));
        }
        public ActionResult DeleteProject(string uid)
        {
            var proj = _context.Project.Where(p => p.Uid == uid).FirstOrDefault <Project>();
            AzureFileService fileService = new AzureFileService(this._appSettings);


            if (proj == null)
            {
                return(BadRequest(new { message = "Invalid project uid." }));
            }

            var perm       = _context.Permissions.Where(p => p.ProjId == proj.Id);
            var param      = _context.ProjectParameters.Where(p => p.ProjectId == proj.Id).ToList <ProjectParameters>();
            var concepts   = _context.Concept.Where(c => c.ProjectId == proj.Id).ToList <Concept>();
            var surveys    = _context.Survey.Where(u => u.ProjectId == proj.Id).ToList <Survey>();
            var prototypes = _context.Prototype.Where(p => p.ProjectId == proj.Id).ToList <Prototype>();

            foreach (Survey u in surveys)
            {
                if (u.Id != null)
                {
                    var answers = _context.SurveyAnswer.Where(a => a.SurveyUid == u.Uid);
                    var takers  = _context.SurveyTaker.Where(t => t.SurveyUid == u.Uid);
                    _context.SurveyAnswer.RemoveRange(answers);
                    _context.SurveyTaker.RemoveRange(takers);
                }
            }

            // foreach (Prototype p in prototypes)
            // {
            //     if (p.Id != null)
            //     {
            //         var result = fileService.DeleteFile(p.PrototypePath, p.PrototypeName);
            //         if (result == true)
            //         {
            //           _context.Prototype.Remove(p);
            //         }
            //     }
            // }

            foreach (Concept c in concepts)
            {
                if (c.Id != null)
                {
                    var iAnswers = _context.IdeationAnswers.Where(i => i.Cid == i.Id);
                    _context.IdeationAnswers.RemoveRange(iAnswers);
                }
            }

            foreach (ProjectParameters p in param)
            {
                if (p.LinkId != null)
                {
                    var link = _context.Links.Where(l => l.Id == p.LinkId).FirstOrDefault <Links>();
                    _context.Links.Remove(link);
                }
            }
            _context.Survey.RemoveRange(surveys);
            _context.Concept.RemoveRange(concepts);
            _context.ProjectParameters.RemoveRange(param);
            _context.Permissions.RemoveRange(perm);
            _context.Project.Remove(proj);
            _context.SaveChanges();
            return(Ok(new { message = "Success!" }));
        }