Ejemplo n.º 1
0
        public async Task <IHttpActionResult> CopyFile(int folderID, FileMaster fileMaster)
        {
            try
            {
                FolderMaster folderMaster = await db.FolderMaster.FindAsync(folderID);

                if (!(bool)folderMaster.UseFlag)
                {
                    return(NotFound());
                }
                // save original file path
                string dirFilePath = Path.Combine(ROOT_PATH + FORLDER);
                if (!Directory.Exists(dirFilePath))
                {
                    Directory.CreateDirectory(dirFilePath);
                }

                //save thumbnail file path
                string dirThumbnailFilePath = Path.Combine(ROOT_PATH + THUMBNAILFORLDER);
                if (!Directory.Exists(dirThumbnailFilePath))
                {
                    Directory.CreateDirectory(dirThumbnailFilePath);
                }

                string fileNameKey           = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                string filePathName          = fileNameKey + fileMaster.FileExtension;
                string thumbnailFilePathName = filePathName.ToLower().Replace(".mp4", ".gif");
                File.Copy(Path.Combine(ROOT_PATH + fileMaster.FileUrl), Path.Combine(dirFilePath, filePathName), true);
                File.Copy(Path.Combine(ROOT_PATH + fileMaster.FileThumbnailUrl), Path.Combine(dirThumbnailFilePath, thumbnailFilePathName), true);

                FileMaster newFile = new FileMaster();
                newFile.GroupID          = fileMaster.GroupID;
                newFile.FolderID         = folderID;
                newFile.UserID           = fileMaster.UserID;
                newFile.FileExtension    = fileMaster.FileExtension;
                newFile.FileType         = fileMaster.FileType;
                newFile.FileName         = fileMaster.FileName;
                newFile.FileUrl          = FORLDER + filePathName;
                newFile.FileThumbnailUrl = THUMBNAILFORLDER + thumbnailFilePathName;
                newFile.UseFlag          = fileMaster.UseFlag;
                newFile.UpdateDate       = DateTime.Now;
                newFile.InsertDate       = DateTime.Now;
                db.FileMaster.Add(newFile);
                await db.SaveChangesAsync();

                return(Ok(newFile));
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> PostFolderMaster(FolderMaster folderMaster)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            folderMaster.UpdateDate = DateTime.Now;
            folderMaster.InsertDate = DateTime.Now;
            folderMaster.UseFlag    = true;
            db.FolderMaster.Add(folderMaster);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = folderMaster.FolderID }, folderMaster));
        }
Ejemplo n.º 3
0
        public async Task <DataModel> GetJSTreeNodeDataByCreate(FolderMaster folderMaster)
        {
            folderMaster.UpdateDate = DateTime.Now;
            folderMaster.InsertDate = DateTime.Now;
            folderMaster.UseFlag    = true;
            db.FolderMaster.Add(folderMaster);
            await db.SaveChangesAsync();

            DataModel jdm = new DataModel();

            jdm.id     = folderMaster.FolderID.ToString();
            jdm.text   = folderMaster.FolderName;
            jdm.parent = folderMaster.FolderParentID == null ? "#" : folderMaster.FolderParentID.ToString();
            //jdm.li_attr = fm;
            return(jdm);
        }
Ejemplo n.º 4
0
        public async Task <DataModel> EditTreeNodeFolder(FolderMaster folderMaster)
        {
            folderMaster.UpdateDate      = DateTime.Now;
            db.Entry(folderMaster).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }
            DataModel jdm = new DataModel();

            jdm.id     = folderMaster.FolderID.ToString();
            jdm.text   = folderMaster.FolderName;
            jdm.parent = folderMaster.FolderParentID == null ? "#" : folderMaster.FolderParentID.ToString();
            //jdm.li_attr = fm;
            return(jdm);
        }
Ejemplo n.º 5
0
        public async Task <IHttpActionResult> CutFile(int folderID, FileMaster fileMaster)
        {
            try
            {
                FolderMaster folderMaster = await db.FolderMaster.FindAsync(folderID);

                if (!(bool)folderMaster.UseFlag)
                {
                    return(NotFound());
                }

                fileMaster.FolderID        = folderID;
                fileMaster.UpdateDate      = DateTime.Now;
                db.Entry(fileMaster).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(Ok(fileMaster));
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }
        }
Ejemplo n.º 6
0
        public async Task <IHttpActionResult> DeleteFolderMaster(int id)
        {
            if (db.FolderMaster.Where(a => a.FolderParentID == id).Count() > 0)
            {
                return(BadRequest("Can not delete folder with child."));
            }
            if (db.FileMaster.Where(a => a.FolderID == id).Count() > 0)
            {
                return(BadRequest("Can not delete folder with files."));
            }

            FolderMaster folderMaster = await db.FolderMaster.FindAsync(id);

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

            db.FolderMaster.Remove(folderMaster);
            await db.SaveChangesAsync();

            return(Ok(folderMaster));
        }
Ejemplo n.º 7
0
        public async Task <IHttpActionResult> UploadFile()
        {
            try
            {
                int          userID       = int.Parse(HttpContext.Current.Request["UserID"]);
                int          groupID      = int.Parse(HttpContext.Current.Request["GroupID"]);
                int          folderID     = int.Parse(HttpContext.Current.Request["FolderID"]);
                FolderMaster folderMaster = await db.FolderMaster.FindAsync(folderID);

                GroupMaster groupMaster = await db.GroupMaster.FindAsync(groupID);

                if (!(bool)folderMaster.UseFlag || !(bool)groupMaster.UseFlag)
                {
                    return(NotFound());
                }

                // save original file path
                string dirFilePath = Path.Combine(ROOT_PATH + FORLDER);
                if (!Directory.Exists(dirFilePath))
                {
                    Directory.CreateDirectory(dirFilePath);
                }

                //save thumbnail file path
                string dirThumbnailFilePath = Path.Combine(ROOT_PATH + THUMBNAILFORLDER);
                if (!Directory.Exists(dirThumbnailFilePath))
                {
                    Directory.CreateDirectory(dirThumbnailFilePath);
                }

                HttpFileCollection fileUploadData        = HttpContext.Current.Request.Files;
                HttpPostedFile     file                  = fileUploadData[0];
                string             fileName              = file.FileName.Trim('"');
                FileInfo           fileInfo              = new FileInfo(fileName);
                string             fileExtension         = fileInfo.Extension.ToLower();
                string             fileType              = "";
                string             fileNameKey           = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                string             filePathName          = fileNameKey + fileExtension;
                string             thumbnailFilePathName = filePathName;
                string             filePath              = Path.Combine(dirFilePath, filePathName);
                string             thumbnailFilePath     = Path.Combine(dirThumbnailFilePath, thumbnailFilePathName);
                if (fileExtension == ".mp4" ||
                    fileExtension == ".wmv" ||
                    fileExtension == ".mpg" ||
                    fileExtension == ".avi" ||
                    fileExtension == ".mpeg" ||
                    fileExtension == ".flv" ||
                    fileExtension == ".mkv" ||
                    fileExtension == ".mov")
                {
                    // save video file
                    fileType = "video";
                    file.SaveAs(filePath);
                    // save gif from mp4
                    string ffmpeg = Path.Combine(ROOT_PATH, "bin/ffmpeg.exe");
                    if (!System.IO.File.Exists(ffmpeg))
                    {
                        return(BadRequest());
                    }
                    thumbnailFilePathName = thumbnailFilePathName.ToLower().Replace(fileExtension, ".gif");
                    Process pcs = new Process();
                    pcs.StartInfo.FileName              = ffmpeg;
                    pcs.StartInfo.Arguments             = " -i " + filePath + " -ss 00:00:00.000 -pix_fmt rgb24 -r 10 -s 320x240 -t 00:00:10.000 " + thumbnailFilePath.Replace(fileExtension, ".gif");
                    pcs.StartInfo.UseShellExecute       = false;
                    pcs.StartInfo.RedirectStandardError = true;
                    pcs.StartInfo.CreateNoWindow        = false;
                    try
                    {
                        pcs.Start();
                        pcs.BeginErrorReadLine();
                        pcs.WaitForExit();
                    }
                    catch
                    {
                        return(BadRequest());
                    }
                    finally
                    {
                        pcs.Close();
                        pcs.Dispose();
                    }
                }
                else
                {
                    // save image file
                    fileType = "image";
                    Image originalImg        = Image.FromStream(file.InputStream);
                    int   thumbnailMaxWidth  = 200;
                    int   thumbnailMaxHeight = 150;
                    Image thumbnailImg;
                    if (originalImg.Width > thumbnailMaxWidth && originalImg.Height <= thumbnailMaxHeight)//宽度比目的图片宽度大,长度比目的图片长度小
                    {
                        thumbnailImg = ImageHelper.GetThumbnailImage(originalImg, thumbnailMaxWidth, (thumbnailMaxWidth * originalImg.Height) / originalImg.Width);
                    }
                    else if (originalImg.Width <= thumbnailMaxWidth && originalImg.Height > thumbnailMaxHeight)//宽度比目的图片宽度小,长度比目的图片长度大
                    {
                        thumbnailImg = ImageHelper.GetThumbnailImage(originalImg, (thumbnailMaxHeight * originalImg.Width) / originalImg.Height, thumbnailMaxHeight);
                    }
                    else if (originalImg.Width <= thumbnailMaxWidth && originalImg.Height <= thumbnailMaxHeight) //长宽比目的图片长宽都小
                    {
                        thumbnailImg = ImageHelper.GetThumbnailImage(originalImg, originalImg.Width, originalImg.Height);
                    }
                    else
                    {
                        if ((thumbnailMaxWidth * originalImg.Height) / originalImg.Width > thumbnailMaxHeight)
                        {
                            thumbnailImg = ImageHelper.GetThumbnailImage(originalImg, (thumbnailMaxHeight * originalImg.Width) / originalImg.Height, thumbnailMaxHeight);
                        }
                        else
                        {
                            thumbnailImg = ImageHelper.GetThumbnailImage(originalImg, thumbnailMaxWidth, (thumbnailMaxWidth * originalImg.Height) / originalImg.Width);
                        }
                    }

                    originalImg.Save(filePath);
                    thumbnailImg.Save(thumbnailFilePath);
                }

                FileMaster fileMaster = new FileMaster();
                fileMaster.GroupID          = groupID;
                fileMaster.FolderID         = folderID;
                fileMaster.UserID           = userID;
                fileMaster.FileExtension    = fileExtension;
                fileMaster.FileType         = fileType;
                fileMaster.FileName         = fileName;
                fileMaster.FileUrl          = FORLDER + filePathName;
                fileMaster.FileThumbnailUrl = THUMBNAILFORLDER + thumbnailFilePathName;
                fileMaster.UseFlag          = true;
                fileMaster.UpdateDate       = DateTime.Now;
                fileMaster.InsertDate       = DateTime.Now;
                db.FileMaster.Add(fileMaster);
                await db.SaveChangesAsync();

                return(Ok(fileMaster));
            }
            catch
            {
                throw;
            }
        }