Пример #1
0
        public async Task <ActionResult <ImageModel> > PutImageData(IFormFile file)
        {
            if (file == null)
            {
                return(BadRequest(new ErrorModel(400, "no image attached")));
            }

            if (!Constants.ALLOWED_CONTENT_TYPES.Contains(file.ContentType))
            {
                return(BadRequest(new ErrorModel(400, "invalid content type")));
            }

            var image = new ImageModel();

            image.OwnerUid = authClaims.UserUid;
            image.BlobName = image.Uid.ToString();
            image.Bucket   = Constants.IMAGE_STORAGE_BUCKET;
            image.Filename = file.FileName;
            image.MimeType = file.ContentType;
            image.Size     = file.Length;
            image.Explicit = false;
            image.Public   = false;

            var stream = file.OpenReadStream();

            image.Md5Hash = FileHashing.GetHash(stream);

            if (await database.GetImageByHash(image.Md5Hash, image.OwnerUid) != null)
            {
                return(BadRequest(new ErrorModel(400, "image already existent")));
            }

            stream.Position = 0;
            var imageMeta = Image.FromStream(stream);

            image.Height = imageMeta.Height;
            image.Width  = imageMeta.Width;

            stream.Position = 0;
            await storage.Put(image.Bucket, image.BlobName, stream, image.Size, image.MimeType);

            await database.Put(image);

            return(Created("image", image));
        }
Пример #2
0
        public async void OnDownloadCompleted(DownloadClient <DownloadItem> sender, DownloadCompletedArgs e, DownloadItem userToken)
        {
            _clients.Add(sender); //클라이언트 큐에 추가


            Downloaded++; //다운로드된 아이템 추가(DownloadFileAsync는 비동기-동기화를 쓰기때문에 InterLocked를 쓸 필요가 없다.)

            DownloadItem di = userToken;
            FileDownloadCompletedArgs args = new FileDownloadCompletedArgs();

            args.DownloadedItem     = di;
            args.DownloadQueue      = _filequeue.Count;
            args.ClientId           = sender.ClientId;
            args.Downloaded         = Downloaded;
            args.DownloadToComplete = DownloadToComplete;
            args.ProgressPercentage = Math.Round((double)(100 * Downloaded) / DownloadToComplete);

            if (di.UseFileIntegrityCheck) // 파일 무결성 검사
            {
                args.DownloadedFileHash = await FileHashing.GetFileHashAsync(di.Path, di.HashType);

                if (di.FileHash != args.DownloadedFileHash)
                {
                    args.FileIntegrity = false;
                }
                else
                {
                    args.FileIntegrity = true;
                }
            }


            if (di.UseAutoRedownloadWhenFileIsCracked) //파일 깨졌다면 재다운로드
            {
                if (!args.FileIntegrity)
                {
                    if (di.UseTempPath)
                    {
                        File.Delete(di.TempPath);
                    }
                    else
                    {
                        File.Delete(di.Path);
                    }
                    EnqueueItem(userToken);
                }
            }

            if (di.UseTempPath)
            {
                string path = Path.GetDirectoryName(di.Path);
                if (!System.IO.Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string temppath = Path.GetDirectoryName(di.TempPath);
                if (!System.IO.Directory.Exists(temppath))
                {
                    Directory.CreateDirectory(temppath);
                }
                File.Delete(di.Path);
                File.Move(di.TempPath, di.Path);
            }
            RaiseDownloadCompleted(args);
            if (Downloaded == DownloadToComplete)
            {
                RaiseAllDownloadCompleted(new AllFileDownloadCompletedArgs());
            }
        }