コード例 #1
0
ファイル: CodeController.cs プロジェクト: center3d/MLW
        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 (!FilePathHelper.IsFileNameValid(formFile.FileName))
                    {
                        return(BadRequest("File name not valid."));
                    }
                    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;

                            case "r":
                                type = "R";
                                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."));
            }
        }
コード例 #2
0
ファイル: CodeController.cs プロジェクト: center3d/MLW
        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)));
        }
コード例 #3
0
ファイル: CodeController.cs プロジェクト: center3d/MLW
        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"];
                    if (!FilePathHelper.IsFileNameValid(newFileName))
                    {
                        return(BadRequest(new { message = "Renaming file failed. Invalid file name." }));
                    }
                    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." }));
        }
コード例 #4
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);
        }
コード例 #5
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);
        }
コード例 #6
0
ファイル: CodeController.cs プロジェクト: vadgama/MLW
        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."));
            }
        }