Пример #1
0
        /// <summary>
        /// Delete record from the payload
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Delete(string id)
        {
            bool result = false;
            //logic to delete
            CodeResponse _code = new CodeResponse();

            foreach (var item in GlobalStorage.CodeStorage)
            {
                try
                {
                    if ((item.Key == id))
                    {
                        File.Delete(item.Value.FilePath);
                        if (item.Value.Type == "JUPYTER_NOTEBOOK")
                        {
                            Directory.Delete(item.Value.FilePath.Replace(item.Value.Name, ""), true);
                        }
                        GlobalStorage.CodeStorage.TryRemove(id, out _code);
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    var err = ex.InnerException;
                }
            }
            return(result);
        }
Пример #2
0
        private static GetSecretResult GetSecretAdvanced(int secretId, Account account, string token, CodeResponse[] codeResponses)
        {
            var soapClient      = GetClient(account);
            var getSecretResult = soapClient.GetSecret(token, secretId, true, codeResponses);

            if (getSecretResult.Errors.Length == 0)
            {
                return(getSecretResult);
            }
            var secretError = getSecretResult.SecretError;

            if (secretError != null && secretError.AllowsResponse)
            {
                Console.WriteLine(secretError.ErrorMessage);
                string comment          = "";
                string addtionalComment = "";
                if (!string.IsNullOrEmpty(secretError.CommentTitle))
                {
                    Console.Write(secretError.CommentTitle + ":");
                    comment = Console.ReadLine();
                }
                if (!string.IsNullOrEmpty(secretError.AdditionalCommentTitle))
                {
                    Console.Write(secretError.AdditionalCommentTitle + ":");
                    addtionalComment = Console.ReadLine();
                }
                var codeResponse = new CodeResponse()
                {
                    ErrorCode = secretError.ErrorCode, Comment = comment, AdditionalComment = addtionalComment
                };
                return(GetSecretAdvanced(secretId, account, token, new CodeResponse[] { codeResponse }));
            }
            return(getSecretResult);
        }
 public Response <T> ResponseFail(CodeResponse code, IList <string> messages)
 {
     ResponseBusiness.TransactionComplete = false;
     ResponseBusiness.ResponseCode        = (int)code;
     ResponseBusiness.Message             = messages;
     return(ResponseBusiness);
 }
Пример #4
0
    IEnumerator Upload(string Codigo)
    {
        WWWForm form = new WWWForm();

        form.AddField("codigo", Codigo);

        UnityWebRequest www = UnityWebRequest.Post("http://40.86.95.152:5000/entrarClase", form);

        yield return(www.SendWebRequest());

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            CodeResponse myCodeClass = JsonUtility.FromJson <CodeResponse>(www.downloadHandler.text);
            Debug.Log(www.downloadHandler.text);

            if (myCodeClass.mensaje == "Correcto")
            {
                SceneManager.LoadScene("Avatar");
            }
            else
            {
                SSTools.ShowMessage("Tu código no esta asociado a ningun maestro", SSTools.Position.bottom, SSTools.Time.threeSecond);
            }
        }
    }
Пример #5
0
        public void TestUpdateCodePayload()
        {
            List <Property>     _prop = new List <Property>();
            List <CodeResponse> _Code = new List <CodeResponse>();

            TestCreateCodePayload();
            _Code = CodePayload.Get();
            _prop.Add(new Property {
                key = "new_prop", value = "200"
            });
            CodeResponse updateRecord = new CodeResponse()
            {
                Id         = "HelloCode",
                Name       = "HelloCode.py",
                User       = "",
                Created_on = DateTime.Now.ToString(),
                Edited_on  = DateTime.Now.ToString(),
                Extension  = ".png",
                MimeType   = "application/image",
                Size       = 111,
                Type       = "PY",
                Url        = "http://localhost/uploads/Code/HelloCode.py",
                FilePath   = "",
                Properties = _prop
            };

            CodeResponse updated = CodePayload.Update(updateRecord);

            Assert.NotEqual(_Code[0].Properties, updateRecord.Properties);
        }
Пример #6
0
        /// <summary>
        /// Delete record from the payload
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Delete(string id)
        {
            bool result = false;
            //logic to delete
            CodeResponse _code = new CodeResponse();

            foreach (var item in GlobalStorage.CodeStorage)
            {
                try
                {
                    if ((item.Key == id))
                    {
                        string resourcePath = item.Value.FilePath;
                        File.Delete(resourcePath);
                        if (item.Value.Type == "JUPYTER_NOTEBOOK" || item.Value.Type == "PYTHON")
                        {
                            string directoryToDelete = resourcePath.Substring(0, resourcePath.LastIndexOf(item.Value.Name));
                            Directory.Delete(directoryToDelete);
                        }
                        GlobalStorage.CodeStorage.TryRemove(id, out _code);
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    var err = ex.InnerException;
                }
            }
            return(result);
        }
Пример #7
0
        public void Test_Code_GetFilePathById()
        {
            //
            List <Property> _prop  = new List <Property>();
            CodeResponse    record = new CodeResponse()
            {
                Id         = "HelloCode",
                Name       = "Hello.py",
                User       = "",
                Created_on = DateTime.Now.ToString(),
                Edited_on  = DateTime.Now.ToString(),
                Extension  = ".py",
                MimeType   = "application/image",
                Size       = 111,
                Type       = "PY",
                Url        = "http://localhost/uploads/Code/Hello.png",
                FilePath   = "some Path",
                Properties = _prop
            };

            List <CodeResponse> records = new List <CodeResponse> {
                record
            };

            string result = FilePathHelper.GetFilePathById("HelloCode", records);

            //
            Assert.Equal("some Path", result);
        }
Пример #8
0
        public async Task <CodeResponse> ExecuteCustomInputAsync(int attendeeId, TestAnswerAC testAnswer)
        {
            var result = new Result();

            if (testAnswer.Code.Input != null)
            {
                result = await ExecuteCodeAsync(testAnswer.Code);
            }

            var codeResponse = new CodeResponse()
            {
                ErrorOccurred       = result.ExitCode != 0,
                Error               = result.Output,
                Message             = null,
                Output              = result.Output,
                TimeConsumed        = result.RunTime,
                MemoryConsumed      = result.MemoryConsumed,
                TotalTestCasePassed = 0,
                TotalTestCases      = 0
            };

            testAnswer.Code.CodeResponse = codeResponse;
            await AddAnswerAsync(attendeeId, testAnswer, 0.0);

            return(codeResponse);
        }
Пример #9
0
        public IActionResult GetContractInfo([FromBody] StringRequest request)
        {
            // Call blockchain to get the smart contract code
            string json = WebRequestHelper.GET("http://localhost:38220/api/SmartContracts/code?address=" + request.request);

            // Convert the JSON to a CodeResponse object
            CodeResponse response = JsonConvert.DeserializeObject <CodeResponse>(json);

            if (response == null || string.IsNullOrEmpty(response.bytecode))
            {
                return(BadRequest());
            }

            // Grab the byte code from the response object
            string contractByteString = response.bytecode;

            // Convert string to byte array
            byte[] contractByteCode = HexToByteArray(contractByteString);

            List <StratisMethodInfo> result = new List <StratisMethodInfo>();

            using (ModuleDefinition moduleDefinition = ModuleDefinition.ReadModule(new MemoryStream(contractByteCode)))
            {
                // Get the contract definition, and all methods on it
                TypeDefinition     contractType = GetContractType(moduleDefinition);
                MethodDefinition[] methods      = contractType.Methods.ToArray();


                foreach (MethodDefinition definition in methods)
                {
                    // No need to show the contracts constructor method
                    if (definition.Name != ".ctor")
                    {
                        // Build a new Method Info project
                        StratisMethodInfo newInfo = new StratisMethodInfo(definition.Name);

                        List <StratisParamterInfo> newParamters = new List <StratisParamterInfo>();

                        // Loop through all paramters and add them the list
                        for (var i = 0; i < definition.Parameters.Count; ++i)
                        {
                            newParamters.Add(
                                new StratisParamterInfo(
                                    definition.Parameters[i].ParameterType.Name,
                                    definition.Parameters[i].Name
                                    )
                                );
                        }

                        // Add the paramters list to the method info object. Then add the object to the result list.
                        newInfo.Paramters = newParamters.ToArray();
                        result.Add(newInfo);
                    }
                }
            }
            return(Ok(result.ToArray()));
        }
Пример #10
0
        public static CodeResponse Update(CodeResponse updatedRecord)
        {
            //
            CodeResponse _m = GlobalStorage.CodeStorage.Values
                              .Where(a => a.Id == updatedRecord.Id)
                              .Select(a => a).ToList()[0];

            //
            Delete(updatedRecord.Id);
            GlobalStorage.CodeStorage.TryAdd(updatedRecord.Id, updatedRecord);
            return(updatedRecord);
        }
Пример #11
0
        public CodeResponse VerifySession(string Token, int Code)
        {
            Session      session  = null;
            CodeResponse response = new CodeResponse
            {
                Success = false,
                Errors  = new List <string>(),
            };

            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Open();

                session = connection
                          .Query <Session>(
                    "EXEC [Support-API].[dbo].[SP_Get_Session] @JWT = @JWT",
                    new { JWT = Token }
                    ).FirstOrDefault();
            }

            Hash   codeHash      = new Hash(session.Code);
            string CodeStr       = Code.ToString();
            string HashedCodeStr = Hashing.GenerateHash(CodeStr, codeHash.iterations, codeHash.salt);

            if (session.Code == HashedCodeStr)
            {
                using (var connection = new SqlConnection(_connectionString))
                {
                    connection.Open();

                    session = connection
                              .Query <Session>(
                        "EXEC [Support-API].[dbo].[SP_Verify_Session] @JWT = @JWT",
                        new { JWT = Token }
                        ).FirstOrDefault();
                }

                if (session.Verified)
                {
                    response.Success = true;
                }
                else
                {
                    response.Errors.Add("Invalid Code");
                }
            }
            else
            {
                response.Errors.Add("Invalid Token");
            }

            return(response);
        }
Пример #12
0
        public static bool RemoveOnlyFromCodePayload(string id)
        {
            bool result = false;

            CodeResponse _data = new CodeResponse();

            foreach (var item in GlobalStorage.CodeStorage)
            {
                GlobalStorage.CodeStorage.TryRemove(id, out _data);
                result = true;
            }
            return(result);
        }
Пример #13
0
        public async Task <IActionResult> Get(string id)
        {
            string       fileContent = string.Empty;
            string       filePath    = FilePathHelper.GetFilePathById <CodeResponse>(id, codeResponse);
            string       fileName    = Path.GetFileName(filePath);
            CodeResponse _meta       = new CodeResponse();
            JObject      jsonObj     = new JObject();

            //get file metadata
            foreach (var record in codeResponse)
            {
                if (record.Id.ToString() == id)
                {
                    fileName = record.Name;
                    _meta    = record;
                }
            }

            try
            {
                if (!string.IsNullOrEmpty(fileName))
                {
                    using (StreamReader reader = new StreamReader(filePath))
                    {
                        fileContent = await reader.ReadToEndAsync();
                    }
                }

                string  strjObj  = JsonConvert.SerializeObject(_meta);
                JObject jObjCode = JObject.Parse(strjObj);

                //JObject jsonObj = JObject.Parse();
                jsonObj.Add("code", fileContent);

                jObjCode.Merge(jsonObj, new JsonMergeSettings
                {
                    // union array values together to avoid duplicates
                    MergeArrayHandling = MergeArrayHandling.Union
                });

                return(Json(jObjCode));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Пример #14
0
        public static CodeResponse GetById(string id)
        {
            CodeResponse result = new CodeResponse();

            if (GlobalStorage.CodeStorage != null)
            {
                foreach (var item in GlobalStorage.CodeStorage)
                {
                    if (item.Key == id)
                    {
                        result = item.Value;
                        break;
                    }
                }
            }
            return(result);
        }
Пример #15
0
        public void TestCreateCodePayload()
        {
            List <Property> _prop     = new List <Property>();
            CodeResponse    newRecord = new CodeResponse()
            {
                Id         = "HelloCode",
                Name       = "Hello.py",
                User       = "",
                Created_on = DateTime.Now.ToString(),
                Edited_on  = DateTime.Now.ToString(),
                Extension  = ".py",
                MimeType   = "application/image",
                Size       = 111,
                Type       = "PY",
                Url        = "http://localhost/uploads/Code/Hello.png",
                FilePath   = "some Path",
                Properties = _prop
            };

            CodeResponse createdRecord = CodePayload.Create(newRecord);

            Assert.Equal(newRecord, createdRecord);
        }
Пример #16
0
        public async Task <CodeResponse> ExecuteCodeSnippetAsync(int attendeeId, bool runOnlyDefault, TestAnswerAC testAnswer)
        {
            var allTestCasePassed = true;
            var countPassedTest   = 0;
            var errorEncounter    = false;
            var errorMessage      = "";
            var score             = 0d;
            var code            = testAnswer.Code;
            var codeResponse    = new CodeResponse();
            var testCases       = new List <CodeSnippetQuestionTestCases>();
            var results         = new List <Result>();
            var testCaseResults = new List <TestCaseResult>();
            var testCaseChecks  = await _dbContext.CodeSnippetQuestion.SingleOrDefaultAsync(x => x.Id == testAnswer.QuestionId);

            var completeTestCases = await _dbContext.CodeSnippetQuestionTestCases.Where(x => x.CodeSnippetQuestionId == testAnswer.QuestionId).ToListAsync();

            //Filter Test Cases
            testCases.AddRange(completeTestCases.Where(x => x.TestCaseType == TestCaseType.Default).ToList());
            if (testCaseChecks.RunBasicTestCase && !runOnlyDefault)
            {
                testCases.AddRange(completeTestCases.Where(x => x.TestCaseType == TestCaseType.Basic).ToList());
            }
            if (testCaseChecks.RunCornerTestCase && !runOnlyDefault)
            {
                testCases.AddRange(completeTestCases.Where(x => x.TestCaseType == TestCaseType.Corner).ToList());
            }
            if (testCaseChecks.RunNecessaryTestCase && !runOnlyDefault)
            {
                testCases.AddRange(completeTestCases.Where(x => x.TestCaseType == TestCaseType.Necessary).ToList());
            }
            //End of filter

            foreach (var testCase in testCases)
            {
                code.Input = testCase.TestCaseInput;

                var result = await ExecuteCodeAsync(code);

                if (result.ExitCode != 0)
                {
                    errorEncounter = true;
                    errorMessage   = result.Output;
                }

                //Trim newline character
                result.Output = result.Output.TrimEnd(new char[] { '\r', '\n' });

                if (result.Output != testCase.TestCaseOutput)
                {
                    allTestCasePassed = false;
                }
                else
                {
                    countPassedTest++;
                    //Calculate score
                    score += testCase.TestCaseMarks;
                }

                var testCaseResult = new TestCaseResult()
                {
                    Memory = result.MemoryConsumed,
                    Output = result.Output,
                    CodeSnippetQuestionTestCasesId = testCase.Id,
                    Processing = result.RunTime
                };
                testCaseResults.Add(testCaseResult);
            }

            //Add score to the TestCodeSolution table
            //Final score is calculated using the following formula:
            //Total score of this question = Sum(1st test case marks + 2nd test case marks .....) / Sum of total marks of all test cases
            var totalTestCaseScore = testCases.Sum(x => x.TestCaseMarks);
            var codeSolution       = new TestCodeSolution()
            {
                TestAttendeeId = attendeeId,
                QuestionId     = testAnswer.QuestionId,
                Solution       = testAnswer.Code.Source,
                Language       = testAnswer.Code.Language,
                Score          = score / totalTestCaseScore
            };
            await _dbContext.TestCodeSolution.AddAsync(codeSolution);

            await _dbContext.SaveChangesAsync();

            //Add result to TestCaseResult Table
            foreach (var testCaseResult in testCaseResults)
            {
                testCaseResult.TestCodeSolution = codeSolution;
            }
            await _dbContext.TestCaseResult.AddRangeAsync(testCaseResults);

            await _dbContext.SaveChangesAsync();

            codeResponse.ErrorOccurred = false;
            if (allTestCasePassed && !errorEncounter)
            {
                codeResponse.Message = runOnlyDefault ? _stringConstants.DefaultTestCasePassed : _stringConstants.AllTestCasePassed;
            }
            else if (!errorEncounter)
            {
                if (countPassedTest > 0)
                {
                    codeResponse.Message = runOnlyDefault ? _stringConstants.DefaultTestCaseFailed : _stringConstants.SomeTestCasePassed;
                }
                else
                {
                    codeResponse.Message = runOnlyDefault ? _stringConstants.DefaultTestCaseFailed : _stringConstants.NoTestCasePassed;
                }
            }
            else
            {
                codeResponse.ErrorOccurred = true;
                codeResponse.Error         = errorMessage;
            }

            //Add answer to the database
            testAnswer.Code.CodeResponse     = codeResponse;
            codeResponse.TotalTestCases      = testCases.Count();
            codeResponse.TotalTestCasePassed = countPassedTest;
            await AddAnswerAsync(attendeeId, testAnswer, 0.0);

            return(codeResponse);
        }
Пример #17
0
        public async Task <IActionResult> Post(List <IFormFile> file)
        {
            List <CodeResponse> response         = new List <CodeResponse>();
            List <CodeResponse> existingCodeData = new List <CodeResponse>();
            long   size = file.Sum(f => f.Length);
            bool   IsFileExists = false;
            string type, dirFullpath, nbPath, _url = string.Empty;
            // full path to file in temp location
            var filePath = Path.GetTempFileName();

            foreach (var formFile in file)
            {
                dirFullpath = DirectoryHelper.GetCodeDirectoryPath();
                if (formFile.Length > 0)
                {
                    //check if the file with the same name exists
                    existingCodeData = CodePayload.Get();
                    if (existingCodeData.Count > 0)
                    {
                        //
                        foreach (var record in existingCodeData)
                        {
                            if (record.Name == formFile.FileName)
                            {
                                IsFileExists = true;
                            }
                        }
                    }
                    existingCodeData.Clear();
                    //
                    if (!IsFileExists)
                    {
                        var fileExt = System.IO.Path.GetExtension(formFile.FileName).Substring(1);
                        if (fileExt.ToLower() == "ipynb")
                        {
                            dirFullpath = dirFullpath + formFile.FileName.Replace(".ipynb", "/");
                            _url        = DirectoryHelper.GetCodeUrl(formFile.FileName.Replace(".ipynb", string.Empty) + "/" + formFile.FileName);
                        }
                        else
                        {
                            _url = DirectoryHelper.GetCodeUrl(formFile.FileName);
                        }
                        nbPath = dirFullpath + formFile.FileName;

                        //check if folder path exists...if not then create folder
                        if (!Directory.Exists(dirFullpath))
                        {
                            Directory.CreateDirectory(dirFullpath);
                        }
                        // upload file start
                        using (var fileStream = new FileStream(Path.Combine(dirFullpath, formFile.FileName), FileMode.Create))
                        {
                            //check file allowed extensions
                            if (!extensions.Contains(fileExt.ToString().ToLower()))
                            {
                                return(BadRequest("File type not allowed"));
                            }

                            //file copy
                            await formFile.CopyToAsync(fileStream);

                            // upload file edn and start creating payload
                            switch (fileExt.ToLower())
                            {
                            case "py":
                                type = "PYTHON";
                                break;

                            case "ipynb":
                                type = "JUPYTER_NOTEBOOK";
                                break;

                            default:
                                type = "UNRECOGNIZED";
                                break;
                            }

                            CodeResponse newRecord = new CodeResponse()
                            {
                                Id          = formFile.FileName.Replace("." + fileExt, ""),
                                Name        = formFile.FileName,
                                User        = "",
                                Created_on  = DateTime.Now.ToString(),
                                Edited_on   = DateTime.Now.ToString(),
                                Extension   = fileExt,
                                MimeType    = formFile.ContentType,
                                Size        = formFile.Length,
                                Type        = type,
                                Url         = _url,
                                FilePath    = nbPath,
                                DateCreated = DateTime.Now
                            };
                            CodePayload.Create(newRecord);
                            response.Add(newRecord);
                        }
                    }
                }
                IsFileExists = false;
            }

            if (response.Count > 0)
            {
                return(Ok(response));
            }
            else
            {
                return(BadRequest("File already exists."));
            }
        }
Пример #18
0
        public async Task <IActionResult> ModifyFilenameAsync(string id)
        {
            string newFileName    = "";
            string newDirFullPath = "";
            string dirFullpath    = DirectoryHelper.GetCodeDirectoryPath();
            string reqBody        = "";
            await System.Threading.Tasks.Task.FromResult(0);

            //
            try
            {
                //read request body
                using (var reader = new StreamReader(Request.Body))
                {
                    var body = reader.ReadToEnd();
                    reqBody = body.ToString();
                }
                //get new filename
                if (!string.IsNullOrEmpty(reqBody))
                {
                    var content = JObject.Parse(reqBody);
                    newFileName = (string)content["newName"];
                    newFileName = Regex.Replace(newFileName, "[\n\r\t]", string.Empty);
                    newFileName = Regex.Replace(newFileName, @"\s", string.Empty);
                }
                //if same name exist - BadRequest
                foreach (var record in codeResponse)
                {
                    if (record.Id.ToLower() == newFileName.ToLower())
                    {
                        return(BadRequest(new { message = "File with same name already exists." }));
                    }
                }

                if (!string.IsNullOrEmpty(newFileName))
                {
                    //rename the file and/or folder
                    foreach (var record in codeResponse)
                    {
                        if (record.Id.ToString() == id)
                        {
                            if (record.Type == "JUPYTER_NOTEBOOK")
                            {
                                //check if folder path exists...if not then move folder
                                newDirFullPath = $"{dirFullpath}{newFileName}";
                                if (!Directory.Exists(newDirFullPath))
                                {
                                    Directory.Move($"{dirFullpath}{id}", newDirFullPath);
                                    FileFolderHelper.RenameFile($"{newDirFullPath}/{id}.{record.Extension}", $"{newDirFullPath}/{newFileName}.{record.Extension}");
                                    newDirFullPath = $"{newDirFullPath}/{newFileName}.{record.Extension}";
                                }
                                else
                                {
                                    return(BadRequest(new { message = "Folder with same name already exists. Please choose different file/folder name." }));
                                }
                            }
                            else
                            {
                                newDirFullPath = record.FilePath.Replace($"{id}.{record.Extension}", $"{newFileName}.{record.Extension}");
                                FileFolderHelper.RenameFile(record.FilePath, newDirFullPath);
                            }
                            //update GlobalStorage dictionary
                            CodeResponse newRecord = new CodeResponse()
                            {
                                Id          = newFileName,
                                Name        = $"{newFileName}.{record.Extension}",
                                User        = "",
                                Created_on  = record.Created_on,
                                Edited_on   = record.Edited_on,
                                Extension   = record.Extension,
                                MimeType    = record.MimeType,
                                Size        = record.Size,
                                Type        = record.Type,
                                Url         = record.Url.Replace(id, newFileName),
                                FilePath    = newDirFullPath,
                                DateCreated = record.DateCreated
                            };
                            CodePayload.Create(newRecord);
                            CodePayload.RemoveOnlyFromCodePayload(id);
                            return(Json(newRecord));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = "Renaming file failed.", exception = ex.StackTrace }));
            }
            return(BadRequest(new { message = "Renaming file failed." }));
        }
Пример #19
0
        public static bool ScanDirectoryToSeed()
        {
            bool   result = false;
            string fileName, _url, _fullName, fileContent, fileExt = "";

            Console.WriteLine("Dir Loc=" + DirectoryHelper.fileUploadDirectoryPath);
            var zmodDir = new ZmodDirectory(DirectoryHelper.fileUploadDirectoryPath);

            //seed data - subdir, csv, img and json
            #region DATA - SUBDIR
            foreach (var subdir in Directory.GetDirectories(DirectoryHelper.GetDataDirectoryPath()))
            {
                string          folderName = Path.GetFileName(subdir);
                string          _createdOn = Directory.GetCreationTime(subdir).ToString();
                List <Property> _props     = new List <Property>();
                _props.Add(new Property {
                    key = "Subdirectories", value = DirectoryHelper.CountDirectories(subdir).ToString()
                });
                _props.Add(new Property {
                    key = "Files", value = DirectoryHelper.CountFiles(subdir).ToString()
                });

                DataResponse newRecord = new DataResponse()
                {
                    Created_on  = Directory.GetCreationTime(subdir).ToString(),
                    Edited_on   = Directory.GetLastWriteTime(subdir).ToString(),
                    Extension   = "",
                    Type        = "FOLDER",
                    FilePath    = subdir,
                    Id          = folderName,
                    MimeType    = "",
                    Name        = folderName,
                    Properties  = _props,
                    DateCreated = Directory.GetCreationTime(subdir)
                };
                //
                DataPayload.Create(newRecord);
            }
            #endregion
            #region DATA - CSV
            foreach (var item in zmodDir.CsvFiles)
            {
                List <Property> _props = new List <Property>();
                fileName  = item.Value.info.Name;
                fileExt   = "csv";
                _fullName = item.Value.info.FullName;
                _fullName = _fullName.Substring(_fullName.IndexOf("Data")).Remove(0, 5);
                _url      = DirectoryHelper.GetDataUrl(_fullName);

                //get properties row and column count
                int[] csvProps = CsvHelper.GetCsvRowColumnCount(item.Value.info.FullName);
                _props.Add(new Property {
                    key = "Number of Rows", value = csvProps[0].ToString()
                });
                _props.Add(new Property {
                    key = "Number of Columns", value = csvProps[1].ToString()
                });
                //
                DataResponse newRecord = new DataResponse()
                {
                    Created_on  = item.Value.info.CreationTime.ToString(),
                    Edited_on   = item.Value.info.LastWriteTime.ToString(),
                    Extension   = fileExt,
                    FilePath    = item.Value.info.FullName,
                    Id          = fileName.Replace($".{fileExt}", ""),
                    MimeType    = "text/csv",
                    Name        = fileName,
                    Properties  = _props,
                    Size        = item.Value.info.Length,
                    Type        = "CSV",
                    Url         = _url,
                    User        = "",
                    DateCreated = item.Value.info.CreationTime
                };
                //
                DataPayload.Create(newRecord);
            }
            #endregion

            #region DATA - IMAGES
            foreach (var item in zmodDir.ImageFiles)
            {
                List <Property> _props = new List <Property>();
                fileName  = item.Value.info.Name;
                fileExt   = item.Value.info.Extension.Remove(0, 1);
                _fullName = item.Value.info.FullName;
                _fullName = _fullName.Substring(_fullName.IndexOf("Data")).Remove(0, 5);
                _url      = DirectoryHelper.GetDataUrl(_fullName).Replace("\\", "/");

                //get properties
                try
                {
                    // using (var image = new Bitmap(System.Drawing.Image.FromFile(item.Value.info.FullName)))
                    using (var image = new Bitmap(item.Value.info.FullName))
                    {
                        _props.Add(new Property {
                            key = "Width", value = image.Width.ToString() + " px"
                        });
                        _props.Add(new Property {
                            key = "Height", value = image.Height.ToString() + " px"
                        });
                        image.Dispose();
                    }
                    //
                    DataResponse newRecord = new DataResponse()
                    {
                        Created_on  = item.Value.info.CreationTime.ToString(),
                        Edited_on   = item.Value.info.LastWriteTime.ToString(),
                        Extension   = fileExt,
                        FilePath    = item.Value.info.FullName,
                        Id          = fileName.Replace($".{fileExt}", ""),
                        MimeType    = $"image/{fileExt}",
                        Name        = fileName,
                        Properties  = _props,
                        Size        = item.Value.info.Length,
                        Type        = "IMAGE",
                        Url         = _url,
                        User        = "",
                        DateCreated = item.Value.info.CreationTime
                    };
                    //
                    DataPayload.Create(newRecord);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.StackTrace);
                }
            }

            #endregion

            #region DATA - JSON

            foreach (var item in zmodDir.JsonFiles)
            {
                List <Property> _props = new List <Property>();
                fileName    = item.Value.info.Name;
                fileExt     = "json";
                _fullName   = item.Value.info.FullName;
                _fullName   = _fullName.Substring(_fullName.IndexOf("Data")).Remove(0, 5);
                _url        = DirectoryHelper.GetDataUrl(_fullName);
                fileContent = "";
                //read json file from filestream
                if (!string.IsNullOrEmpty(fileName))
                {
                    using (StreamReader reader = new StreamReader(item.Value.info.FullName))
                    {
                        fileContent = reader.ReadToEnd();
                    }
                }

                //parse
                try
                {
                    if (!string.IsNullOrEmpty(fileContent))
                    {
                        JsonTextReader reader = new JsonTextReader(new StringReader(fileContent));
                        int            objCtr = 0;
                        while (reader.Read())
                        {
                            if (reader.TokenType == JsonToken.EndObject)
                            {
                                objCtr++;
                            }
                        }
                        _props.Add(new Property {
                            key = "Number of Objects", value = objCtr.ToString()
                        });

                        //
                        DataResponse newRecord = new DataResponse()
                        {
                            Created_on  = item.Value.info.CreationTime.ToString(),
                            Edited_on   = item.Value.info.LastWriteTime.ToString(),
                            Extension   = fileExt,
                            FilePath    = item.Value.info.FullName,
                            Id          = fileName.Replace($".{fileExt}", ""),
                            MimeType    = "application/json",
                            Name        = fileName,
                            Properties  = _props,
                            Size        = item.Value.info.Length,
                            Type        = "JSON",
                            Url         = _url,
                            User        = "",
                            DateCreated = item.Value.info.CreationTime
                        };
                        //
                        DataPayload.Create(newRecord);
                    }
                }
                catch (Exception ex)
                {
                    //TODO: logger
                    string err = ex.StackTrace;
                }
            }

            #endregion

            #region DATA - MP4
            foreach (var item in zmodDir.VideoFiles)
            {
                List <Property> _props = new List <Property>();
                fileName  = item.Value.info.Name;
                fileExt   = item.Value.info.Extension.Remove(0, 1);
                _fullName = item.Value.info.FullName;
                _fullName = _fullName.Substring(_fullName.IndexOf("Data")).Remove(0, 5);
                _url      = DirectoryHelper.GetDataUrl(_fullName);

                //get properties
                try
                {
                    DataResponse newRecord = new DataResponse()
                    {
                        Created_on  = item.Value.info.CreationTime.ToString(),
                        Edited_on   = item.Value.info.LastWriteTime.ToString(),
                        Extension   = fileExt,
                        FilePath    = item.Value.info.FullName,
                        Id          = fileName.Replace($".{fileExt}", ""),
                        MimeType    = $"video/{fileExt}",
                        Name        = fileName,
                        Properties  = _props,
                        Size        = item.Value.info.Length,
                        Type        = "VIDEO",
                        Url         = _url,
                        User        = "",
                        DateCreated = item.Value.info.CreationTime
                    };
                    //
                    DataPayload.Create(newRecord);
                }
                catch (Exception ex)
                {
                    //TODO: logger
                    string err = ex.StackTrace;
                }
            }
            #endregion

            #region  DATA - TEXT
            foreach (var item in zmodDir.TextFiles)
            {
                List <Property> _props = new List <Property>();
                fileName  = item.Value.info.Name;
                fileExt   = item.Value.info.Extension.Remove(0, 1);
                _fullName = item.Value.info.FullName;
                _fullName = _fullName.Substring(_fullName.IndexOf("Data")).Remove(0, 5);
                _url      = DirectoryHelper.GetDataUrl(_fullName);

                //get properties
                try
                {
                    DataResponse newRecord = new DataResponse()
                    {
                        Created_on  = item.Value.info.CreationTime.ToString(),
                        Edited_on   = item.Value.info.LastWriteTime.ToString(),
                        Extension   = fileExt,
                        FilePath    = item.Value.info.FullName,
                        Id          = fileName.Replace($".{fileExt}", ""),
                        MimeType    = $"text/plain",
                        Name        = fileName,
                        Properties  = _props,
                        Size        = item.Value.info.Length,
                        Type        = "TEXT",
                        Url         = _url,
                        User        = "",
                        DateCreated = item.Value.info.CreationTime
                    };
                    //
                    DataPayload.Create(newRecord);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.StackTrace);
                }
            }
            #endregion
            //seed code - py and ipynb
            #region  CODE - PY

            foreach (var item in zmodDir.PyFiles)
            {
                fileName = item.Value.info.Name;
                fileExt  = "py";
                _url     = DirectoryHelper.GetCodeUrl(item.Value.info.Name);
                CodeResponse newRecord = new CodeResponse()
                {
                    Created_on  = item.Value.info.CreationTime.ToString(),
                    Edited_on   = item.Value.info.LastWriteTime.ToString(),
                    Extension   = fileExt,
                    FilePath    = item.Value.info.FullName,
                    Id          = fileName.Replace($".{fileExt}", ""),
                    MimeType    = "application/octet-stream",
                    Name        = fileName,
                    Size        = item.Value.info.Length,
                    Type        = "PYTHON",
                    Url         = _url,
                    User        = "",
                    DateCreated = item.Value.info.CreationTime
                };
                CodePayload.Create(newRecord);
            }



            #endregion

            #region  CODE - IPYNB
            foreach (var item in zmodDir.IpynbFiles)
            {
                fileName = item.Value.info.Name;
                fileExt  = Path.GetExtension(fileName).Remove(0, 1);
                _url     = DirectoryHelper.GetCodeUrl(item.Value.info.Name);

                CodeResponse newRecord = new CodeResponse()
                {
                    Created_on  = item.Value.info.CreationTime.ToString(),
                    Edited_on   = item.Value.info.LastWriteTime.ToString(),
                    Extension   = fileExt,
                    FilePath    = item.Value.info.FullName,
                    Id          = fileName.Replace($".{fileExt}", ""),
                    MimeType    = "application/octet-stream",
                    Name        = fileName,
                    Size        = item.Value.info.Length,
                    Type        = "JUPYTER_NOTEBOOK",
                    Url         = _url,
                    User        = "",
                    DateCreated = item.Value.info.CreationTime
                };
                CodePayload.Create(newRecord);
            }
            #endregion

            #region  DATA - R
            foreach (var item in zmodDir.RFiles)
            {
                fileName = item.Value.info.Name;
                fileExt  = "r";
                _url     = DirectoryHelper.GetCodeUrl(item.Value.info.Name);
                CodeResponse newRecord = new CodeResponse()
                {
                    Created_on  = item.Value.info.CreationTime.ToString(),
                    Edited_on   = item.Value.info.LastWriteTime.ToString(),
                    Extension   = fileExt,
                    FilePath    = item.Value.info.FullName,
                    Id          = Path.GetFileNameWithoutExtension(fileName),
                    MimeType    = "application/octet-stream",
                    Name        = fileName,
                    Size        = item.Value.info.Length,
                    Type        = "R",
                    Url         = _url,
                    User        = "",
                    DateCreated = item.Value.info.CreationTime
                };
                CodePayload.Create(newRecord);
            }
            #endregion

            //loop of model
            #region  MODEL - PMML
            foreach (var item in zmodDir.PmmlFiles)
            {
                List <Property> _props = new List <Property>();
                fileName = item.Value.info.Name;
                fileExt  = "pmml";
                _url     = DirectoryHelper.GetModelUrl(item.Value.info.Name);
                //
                ModelResponse newRecord = new ModelResponse()
                {
                    Created_on  = item.Value.info.CreationTime.ToString(),
                    Deployed    = false,
                    Edited_on   = item.Value.info.LastWriteTime.ToString(),
                    Extension   = fileExt,
                    FilePath    = item.Value.info.FullName,
                    Id          = fileName.Replace($".{fileExt}", ""),
                    Loaded      = false,
                    MimeType    = "application/octet-stream",
                    Name        = fileName,
                    Size        = item.Value.info.Length,
                    Type        = "PMML",
                    Url         = _url,
                    User        = "",
                    Properties  = _props,
                    DateCreated = item.Value.info.CreationTime
                };
                ModelPayload.Create(newRecord);
            }
            #endregion
            #region  MODEL - H5
            foreach (var item in zmodDir.H5Files)
            {
                List <Property> _props = new List <Property>();
                fileName = item.Value.info.Name;
                fileExt  = "h5";
                _url     = DirectoryHelper.GetModelUrl(item.Value.info.Name);
                //
                ModelResponse newRecord = new ModelResponse()
                {
                    Created_on  = item.Value.info.CreationTime.ToString(),
                    Deployed    = false,
                    Edited_on   = item.Value.info.LastWriteTime.ToString(),
                    Extension   = fileExt,
                    FilePath    = item.Value.info.FullName,
                    Id          = fileName.Replace($".{fileExt}", ""),
                    Loaded      = false,
                    MimeType    = "application/octet-stream",
                    Name        = fileName,
                    Size        = item.Value.info.Length,
                    Type        = "H5",
                    Url         = _url,
                    User        = "",
                    Properties  = _props,
                    DateCreated = item.Value.info.CreationTime
                };
                ModelPayload.Create(newRecord);
            }
            #endregion

            return(result);
        }
Пример #20
0
        public static bool ScanCodeDirectory()
        {
            bool   result = false;
            string fileName, _url, fileExt = "";

            Console.WriteLine("Dir Loc=" + DirectoryHelper.fileUploadDirectoryPath);
            var zmodDir = new ZmodDirectory(DirectoryHelper.fileUploadDirectoryPath);

            //seed code - py and ipynb
            #region  CODE - PY

            foreach (var item in zmodDir.PyFiles)
            {
                fileName = item.Value.info.Name;
                fileExt  = "py";
                _url     = DirectoryHelper.GetCodeUrl(item.Value.info.Name);
                CodeResponse newRecord = new CodeResponse()
                {
                    Created_on  = item.Value.info.CreationTime.ToString(),
                    Edited_on   = item.Value.info.LastWriteTime.ToString(),
                    Extension   = fileExt,
                    FilePath    = item.Value.info.FullName,
                    Id          = fileName.Replace($".{fileExt}", ""),
                    MimeType    = "application/octet-stream",
                    Name        = fileName,
                    Size        = item.Value.info.Length,
                    Type        = "PYTHON",
                    Url         = _url,
                    User        = "",
                    DateCreated = item.Value.info.CreationTime
                };
                CodePayload.Create(newRecord);
            }



            #endregion
            #region  CODE - IPYNB
            foreach (var item in zmodDir.IpynbFiles)
            {
                fileName = item.Value.info.Name;
                fileExt  = Path.GetExtension(fileName).Remove(0, 1);
                _url     = DirectoryHelper.GetCodeUrl(item.Value.info.Name);

                CodeResponse newRecord = new CodeResponse()
                {
                    Created_on  = item.Value.info.CreationTime.ToString(),
                    Edited_on   = item.Value.info.LastWriteTime.ToString(),
                    Extension   = fileExt,
                    FilePath    = item.Value.info.FullName,
                    Id          = fileName.Replace($".{fileExt}", ""),
                    MimeType    = "application/octet-stream",
                    Name        = fileName,
                    Size        = item.Value.info.Length,
                    Type        = "JUPYTER_NOTEBOOK",
                    Url         = _url,
                    User        = "",
                    DateCreated = item.Value.info.CreationTime
                };
                CodePayload.Create(newRecord);
            }
            #endregion

            return(result);
        }
Пример #21
0
        public async Task <IActionResult> CreateFileAsync(string type)
        {
            if (string.IsNullOrEmpty(type))
            {
                return(BadRequest(new{ message = "Type is needed." }));
            }
            await System.Threading.Tasks.Task.FromResult(0);

            #region Variables
            string          _type       = type.ToLower();
            long            fileSize    = 0L;
            string          dirFullpath = DirectoryHelper.GetCodeDirectoryPath();
            string          _id         = $"New_{DateTime.Now.Ticks.ToString()}";
            string          newFile     = $"{_id}.{_type}";
            string          _url        = DirectoryHelper.GetCodeUrl(newFile);
            string          _filePath   = Path.Combine(dirFullpath, newFile);
            CodeResponse    _code       = new CodeResponse();
            List <Property> _props      = new List <Property>();
            StringBuilder   fileContent = new StringBuilder();
            #endregion
            //
            switch (_type)
            {
            case "py":
                fileContent.Append("print ('Your code goes here')");
                break;

            case "ipynb":
                fileContent.Append("{");
                fileContent.Append("\"cells\": [");
                fileContent.Append("{");
                fileContent.Append("\"cell_type\": \"code\",");
                fileContent.Append("\"execution_count\": null,");
                fileContent.Append("\"metadata\": {},");
                fileContent.Append("\"outputs\": [],");
                fileContent.Append("\"source\": []");
                fileContent.Append("}");
                fileContent.Append(" ],");
                fileContent.Append("\"metadata\": {");
                fileContent.Append("\"kernelspec\": {");
                fileContent.Append("\"display_name\": \"Python 3\",");
                fileContent.Append("\"language\": \"python\",");
                fileContent.Append("\"name\": \"python3\"");
                fileContent.Append("}");
                fileContent.Append("},");
                fileContent.Append("\"nbformat\": 4,");
                fileContent.Append("\"nbformat_minor\": 2");
                fileContent.Append("}");
                dirFullpath = $"{dirFullpath}{_id}/";
                if (!Directory.Exists(dirFullpath))
                {
                    Directory.CreateDirectory(dirFullpath);
                    _filePath = $"{dirFullpath}{newFile}";
                }
                break;
            }

            using (StreamWriter writer = new StreamWriter(_filePath))
            {
                foreach (string line in fileContent.ToString().Split(",@,"))
                {
                    writer.WriteLine(line);
                }

                writer.Flush();
                fileSize = writer.BaseStream.Length;
            }
            //
            CodeResponse newRecord = new CodeResponse()
            {
                Id          = _id,
                Name        = newFile,
                Created_on  = DateTime.Now.ToString(),
                Edited_on   = DateTime.Now.ToString(),
                Extension   = _type,
                MimeType    = "application/octet-stream",
                Size        = fileSize,
                Type        = _type == "ipynb"? "JUPYTER_NOTEBOOK" : "PYTHON",
                Url         = _url,
                FilePath    = _filePath,
                DateCreated = DateTime.Now
            };
            CodePayload.Create(newRecord);

            return(Json(CodePayload.GetById(_id)));
        }
Пример #22
0
        public async Task <IActionResult> Post(List <IFormFile> file)
        {
            List <CodeResponse> response         = new List <CodeResponse>();
            List <CodeResponse> existingCodeData = new List <CodeResponse>();
            long   size = file.Sum(f => f.Length);
            bool   IsFileExists = false;
            string type, dirFullpath, nbPath, _url = string.Empty;
            // full path to file in temp location
            var filePath = Path.GetTempFileName();

            foreach (var formFile in file)
            {
                dirFullpath = DirectoryHelper.GetCodeDirectoryPath();
                #region type
                var fileExt = System.IO.Path.GetExtension(formFile.FileName).Substring(1);
                // upload file edn and start creating payload
                switch (fileExt.ToLower())
                {
                case "py":
                    type = "PYTHON";
                    break;

                case "ipynb":
                    type = "JUPYTER_NOTEBOOK";
                    break;

                case "r":
                    type = "R";
                    break;

                default:
                    type = "UNRECOGNIZED";
                    break;
                }
                #endregion
                if (formFile.Length > 0)
                {
                    //check if the file with the same name exists
                    existingCodeData = CodePayload.Get();
                    if (existingCodeData.Count > 0)
                    {
                        //
                        foreach (var record in existingCodeData)
                        {
                            if (record.Name == formFile.FileName)
                            {
                                IsFileExists = true;
                            }
                        }
                    }
                    existingCodeData.Clear();
                    //
                    if (!FilePathHelper.IsFileNameValid(formFile.FileName))
                    {
                        return(BadRequest("File name not valid."));
                    }
                    if (!IsFileExists)
                    {
                        #region upload large file > 400MB
                        if (size > 40000000)
                        {
                            Console.WriteLine(">>>>>>>>>>>>>>>>>>>>UPLOADING LARGE CODE FILE................................");
                            #region add to uploading
                            //
                            FilesInProgress wip = new FilesInProgress()
                            {
                                Id           = formFile.FileName,
                                CreatedAt    = DateTime.Now,
                                Name         = formFile.FileName,
                                Type         = type,
                                Module       = "CODE",
                                UploadStatus = "INPROGRESS"
                            };

                            FilesUploadingPayload.Create(wip);

                            #endregion
                            //check if same job is scheduled
                            ISchedulerFactory schfack   = new StdSchedulerFactory();
                            IScheduler        scheduler = await schfack.GetScheduler();

                            var jobKey = new JobKey(filePath);
                            if (await scheduler.CheckExists(jobKey))
                            {
                                await scheduler.ResumeJob(jobKey);
                            }
                            else
                            {
                                #region create quartz job for training model
                                ITrigger trigger = TriggerBuilder.Create()
                                                   .WithIdentity($"Uploading Code file-{DateTime.Now}")
                                                   .WithPriority(1)
                                                   .Build();

                                IJobDetail job = JobBuilder.Create <UploadJob>()
                                                 .WithIdentity(filePath)
                                                 .Build();

                                job.JobDataMap["id"]       = formFile.FileName.Replace($".{fileExt}", "");
                                job.JobDataMap["filePath"] = filePath;
                                await _scheduler.ScheduleJob(job, trigger);

                                #endregion
                            }
                        }
                        #endregion
                        if (fileExt.ToLower() == "ipynb")
                        {
                            dirFullpath = dirFullpath + formFile.FileName.Replace(".ipynb", "/");
                            _url        = DirectoryHelper.GetCodeUrl(formFile.FileName.Replace(".ipynb", string.Empty) + "/" + formFile.FileName);
                        }
                        else
                        {
                            _url = DirectoryHelper.GetCodeUrl(formFile.FileName);
                        }
                        nbPath = dirFullpath + formFile.FileName;

                        //check if folder path exists...if not then create folder
                        if (!Directory.Exists(dirFullpath))
                        {
                            Directory.CreateDirectory(dirFullpath);
                        }
                        // upload file start
                        using (var fileStream = new FileStream(Path.Combine(dirFullpath, formFile.FileName), FileMode.Create))
                        {
                            //check file allowed extensions
                            if (!extensions.Contains(fileExt.ToString().ToLower()))
                            {
                                return(BadRequest("File type not allowed"));
                            }

                            //file copy
                            await formFile.CopyToAsync(fileStream);

                            CodeResponse newRecord = new CodeResponse()
                            {
                                Id          = formFile.FileName.Replace("." + fileExt, ""),
                                Name        = formFile.FileName,
                                User        = "",
                                Created_on  = DateTime.Now.ToString(),
                                Edited_on   = DateTime.Now.ToString(),
                                Extension   = fileExt,
                                MimeType    = formFile.ContentType,
                                Size        = formFile.Length,
                                Type        = type,
                                Url         = _url,
                                FilePath    = nbPath,
                                DateCreated = DateTime.Now
                            };
                            CodePayload.Create(newRecord);
                            response.Add(newRecord);
                        }
                    }
                    else
                    {
                        return(Conflict(new { message = "File already exists.", error = "File already exists." }));
                    }
                }
                IsFileExists = false;
            }

            if (response.Count > 0)
            {
                return(Ok(response));
            }
            else
            {
                return(BadRequest("File already exists."));
            }
        }
Пример #23
0
        public async Task <CodeResponse> ModifyArtwork([FromBody] ArtworkModifyInputDto inputDto)
        {
            var result = await _artworkAppService.ModifyArtwork(inputDto);

            return(result ? CodeResponse.Ok() : CodeResponse.Error());
        }
Пример #24
0
        public IActionResult VerifyCode([FromBody] CodePost post)
        {
            CodeResponse response = _sessionManager.VerifySession(post.Token, post.Code);

            return(Ok(response));
        }
Пример #25
0
 /// <summary>
 /// CreateCodePayload
 /// </summary>
 /// <param name="newRecord"></param>
 /// <returns></returns>
 public static CodeResponse Create(CodeResponse newRecord)
 {
     GlobalStorage.CodeStorage.TryAdd(newRecord.Id, newRecord);
     return(newRecord);
 }
Пример #26
0
        public async Task <CodeResponse> RemoveArtwork(int id)
        {
            var result = await _artworkAppService.RemoveArtwork(id);

            return(result ? CodeResponse.Ok() : CodeResponse.Error());
        }
Пример #27
0
        public async Task <CodeResponse> AddOrUpdate(PersonalIntroDto dto)
        {
            var result = await _personalIntroAppService.AddOrUpdate(dto);

            return(result ? CodeResponse.Ok() : CodeResponse.Error());
        }