github.com/minio/console@v1.4.1/web-app/src/screens/Console/ObjectBrowser/utils.ts (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2023 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  import { encodeURLString, getClientOS } from "../../../common/utils";
    18  import { makeid, storeCallForObjectWithID } from "./transferManager";
    19  import { download } from "../Buckets/ListBuckets/Objects/utils";
    20  import {
    21    cancelObjectInList,
    22    completeObject,
    23    failObject,
    24    setLongFileOpen,
    25    setNewObject,
    26    updateProgress,
    27  } from "./objectBrowserSlice";
    28  import { AppDispatch } from "../../../store";
    29  import { setSnackBarMessage } from "../../../systemSlice";
    30  import { BucketObject } from "api/consoleApi";
    31  
    32  export const downloadObject = (
    33    dispatch: AppDispatch,
    34    bucketName: string,
    35    internalPaths: string,
    36    object: BucketObject,
    37  ) => {
    38    const identityDownload = encodeURLString(
    39      `${bucketName}-${object.name}-${new Date().getTime()}-${Math.random()}`,
    40    );
    41  
    42    const isWinOs = getClientOS().toLowerCase().includes("win");
    43  
    44    if ((object.name?.length || 0) > 200 && isWinOs) {
    45      dispatch(setLongFileOpen(true));
    46      return;
    47    }
    48  
    49    const ID = makeid(8);
    50  
    51    const downloadCall = download(
    52      bucketName,
    53      internalPaths,
    54      object.version_id,
    55      object.size || 0,
    56      null,
    57      ID,
    58      (progress) => {
    59        dispatch(
    60          updateProgress({
    61            instanceID: identityDownload,
    62            progress: progress,
    63          }),
    64        );
    65      },
    66      () => {
    67        dispatch(completeObject(identityDownload));
    68      },
    69      (msg: string) => {
    70        dispatch(failObject({ instanceID: identityDownload, msg }));
    71      },
    72      () => {
    73        dispatch(cancelObjectInList(identityDownload));
    74      },
    75      () => {
    76        dispatch(
    77          setSnackBarMessage(
    78            "File download will be handled directly by the browser.",
    79          ),
    80        );
    81      },
    82    );
    83  
    84    storeCallForObjectWithID(ID, downloadCall);
    85    dispatch(
    86      setNewObject({
    87        ID,
    88        bucketName,
    89        done: false,
    90        instanceID: identityDownload,
    91        percentage: 0,
    92        prefix: object.name || "",
    93        type: "download",
    94        waitingForFile: true,
    95        failed: false,
    96        cancelled: false,
    97        errorMessage: "",
    98      }),
    99    );
   100  };