Пример #1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// 时间:2015-12-17 11:13
 /// 备注:
 public WebUploadFile()
 {
     uploadFileSetting = new UploadFileConfig()
     {
         FileDirectory    = "/upload",
         FileType         = ".pdf,.xls,.xlsx,.doc,.docx,.txt,.png,.jpg,.gif",
         MaxSizeM         = 10,
         PathSaveType     = UploadFileSaveType.DateTimeNow,
         IsRenameSameFile = true
     };
 }
Пример #2
0
    public async Task <ContentView> UploadFile(ContentView newView, UploadFileConfig fileConfig, Stream fileData, long requester)
    {
        fileData.Seek(0, SeekOrigin.Begin);

        if (fileData.Length == 0)
        {
            throw new RequestException("No data uploaded!");
        }

        if (fileConfig.quantize >= 0 && (fileConfig.quantize < config.MinQuantize || fileConfig.quantize > config.MaxQuantize))
        {
            throw new RequestException($"Quantize must be between {config.MinQuantize} and {config.MaxQuantize}");
        }

        int trueQuantize = 0;
        var tempLocation = GetAndMakeTempPath(); //Work is done locally for quantize/etc

        var manipResult = await imageManip.FitToSizeAndSave(fileData, tempLocation, config.MaxSize);

        newView.literalType = manipResult.MimeType;
        AddImageRender(manipResult.RenderCount + manipResult.LoadCount);

        try
        {
            var meta = new Dictionary <string, object>()
            {
                { "size", manipResult.SizeInBytes },
                { "width", manipResult.Width },
                { "height", manipResult.Height }
            };

            //OK the quantization step. This SHOULD modify the view for us!
            if (fileConfig.quantize > 0)
            {
                trueQuantize = await TryQuantize(fileConfig.quantize, newView, tempLocation);
            }

            if (trueQuantize > 0)
            {
                meta["quantize"] = trueQuantize;
            }

            //We now have the metadata
            newView.meta = JsonConvert.SerializeObject(meta);

            using var tempWriter = dbFactory.CreateWriter();
            //This is QUITE dangerous: a file could be created in the api first and THEN the file write fails!
            newView = await tempWriter.WriteAsync(newView, requester);

            try
            {
                await SaveMainDataAsync(tempLocation, newView.hash, newView.literalType ?? "");
            }
            catch (Exception ex)
            {
                logger.LogError($"FILE WRITE '{newView.hash}'({newView.id}) FAILED: {ex}");
                await tempWriter.DeleteAsync <ContentView>(newView.id, requester);
            }
        }
        finally
        {
            System.IO.File.Delete(tempLocation);
        }

        //The view is already done.
        return(newView);
    }