Exemple #1
0
        public ActionResult <HostedFile> EditHostedFile(int id, int hfid, [FromBody] HostedFile hostedFile)
        {
            HttpListener listener = (HttpListener)_context.Listeners.FirstOrDefault(L => L.Id == id);

            if (listener == null)
            {
                return(NotFound($"NotFound - HttpListener with id: {id}"));
            }
            HostedFile file = _context.HostedFiles.FirstOrDefault(HF => HF.ListenerId == listener.Id && HF.Id == hfid && HF.Id == hostedFile.Id);

            if (file == null)
            {
                return(NotFound($"NotFound - HostedFile with id: {hfid} and Listener id: {id}"));
            }
            try
            {
                hostedFile = listener.HostFile(hostedFile);
            }
            catch
            {
                return(BadRequest($"BadRequest - Error hosting file at: {hostedFile.Path}"));
            }
            file.Path    = hostedFile.Path;
            file.Content = hostedFile.Path;
            _context.HostedFiles.Update(file);
            _context.SaveChanges();

            return(file);
        }
Exemple #2
0
        public ActionResult <HostedFile> CreateHostedFile(int id, [FromBody] HostedFile hostFileRequest)
        {
            HttpListener listener = (HttpListener)_context.Listeners.FirstOrDefault(L => L.Id == id);

            if (listener == null)
            {
                return(NotFound($"NotFound - HttpListener with id: {id}"));
            }
            hostFileRequest.ListenerId = listener.Id;
            HostedFile existingHostedFile = _context.HostedFiles.FirstOrDefault(HF => HF.Path == hostFileRequest.Path);

            if (existingHostedFile != null)
            {
                // If file already exists and is being hosted, BadRequest
                return(BadRequest($"BadRequest - HostedFile already exists at: {hostFileRequest.Path}"));
            }
            try
            {
                hostFileRequest = listener.HostFile(hostFileRequest);
            }
            catch
            {
                return(BadRequest($"BadRequest - Error hosting file at: {hostFileRequest.Path}"));
            }
            // Check if it already exists again, path could have changed
            existingHostedFile = _context.HostedFiles.FirstOrDefault(HF => HF.Path == hostFileRequest.Path);
            if (existingHostedFile != null)
            {
                return(BadRequest($"BadRequest - HostedFile already exists at: {existingHostedFile.Path}"));
            }
            _context.Indicators.Add(new FileIndicator
            {
                FileName = hostFileRequest.Path.Split("/").Last(),
                FilePath = listener.Url + hostFileRequest.Path,
                MD5      = Encrypt.Utilities.GetMD5(Convert.FromBase64String(hostFileRequest.Content)),
                SHA1     = Encrypt.Utilities.GetSHA1(Convert.FromBase64String(hostFileRequest.Content)),
                SHA2     = Encrypt.Utilities.GetSHA256(Convert.FromBase64String(hostFileRequest.Content))
            });
            _context.HostedFiles.Add(hostFileRequest);
            _context.SaveChanges();

            return(CreatedAtRoute(nameof(GetHostedFile), new { id = listener.Id, hfid = hostFileRequest.Id }, hostFileRequest));
        }