반응형
reduce(callbackFn, initialValue)
  • initiallValue없으면? If initialValue is not specified, previousValue is initialized to the first value in the array, and currentValue is initialized to the second value in the array.
  • initialValue없고 대상array내 값이 1개뿐이라면?
    • 뭘해도 [A] -> reduce(...) -> A 형태로 결과가 나옴, 즉 1개 있던 value가 array를 빠져나오게됨

 

reduce((previousValue, currentValue, currentIndex, array) => { /* … */ }, initialValue)
  • previousValue: accumulated values until now
반응형
반응형

frontend

image upload시 서버 전송 하기 전에 frontend 로직진행하는 부분

<input
        accept="image/*"
        id={`input_${type}`}
        type="file"
        className="logo_preview_input"
        onChange={e => {
          if (e.target.files && e.target.files.length >= 1) {
            const reader = new FileReader();
            const img = e.target.files[0];
            reader.readAsDataURL(img);
            reader.onload = e2 => {
              setImg(e2.target.result);
              setImgName(img.name);
            };
          }
        }}
      />
      <label htmlFor={`input_${type}`} className="btn_logo_preview">
        <Button component="span" className="btn_color4">
          Select File
        </Button>
      </label>
      <p className="file_name">{imgName}</p>

 

업로드된 파일을 서버전송할 때 new FormData()를 써서 이안에 image file이랑 다른 string이랑 같이넣어줌

        const bgImg = document.getElementById('input_bg').files[0];//undefined if not uploaded
        //if (!bgImg) alert('Please select a background image file to update !');
        const previewImg = document.getElementById('input_preview').files[0];//undefined if not uploaded
        //if (!previewImg) alert('Please select a preview image file to update !');
        const buttonImg = document.getElementById('input_button').files[0];
        requestBody = new FormData();
        requestBody.append('tabName', content.tabName);
        requestBody.append('tabOrder', content.tabOrder);
        requestBody.append('themeApply', content.themeApply ? 'Y' : 'N');
        if (bgImg) requestBody.append('bgImg', bgImg);
        if (previewImg) requestBody.append('previewImg', previewImg);
        if (buttonImg) requestBody.append('buttonImg', buttonImg);

전송시 header는 보통 multipart/form-data를 사용하는 것 같은데 왜인지 이렇게 보내면 에러가 났고 아예 설정안하고 보내니 정상동작하였음

//const headers = new Headers({ 'Content-Type': 'application/json' });
//const headers = new Headers({ 'Content-Type': 'multipart/form-data' });

backend

  • formData로 보낸 것들도 url?q=형식으로 get으로 보낸것을 받는 것처럼 @RequestParam으로 받음
  • 그 중 이미지는 org.springframework.web.multipart.MultipartFile로 받음
  • 이미지외에 string으로 보낸 것들은 Map<String, Object>로 몽땅 받음
  • 만약 이름은 bgImg, previewImg이렇게 맞춰서 보냈는데 실제로는 파일이 아니라 string을 보내면 null로 받아짐
@PatchMapping("tab_withImg/{tabId}")
  public Tab updateTabWithImg(@CurrentUser UserPrincipal currentUser, @PathVariable String tabId,
      @RequestParam Map<String, Object> data, @RequestParam(required = false) MultipartFile bgImg,
      @RequestParam(required = false) MultipartFile previewImg, @RequestParam(required = false) MultipartFile buttonImg)
      throws Exception {
    logger.info("#updateTab_WithImg|currentUser=" + currentUser.getProvider());
    return homeAppAdminService.updateTab(tabId, data, bgImg, previewImg, buttonImg);
  }

 

  • multipart file을 받아서 로컬내 저장하고 처리하는 부분
  • 일반적으로 File쓰는 방법으로 저장하면 에러가 나서 java.nio.file.Path/Paths 써서 처리했었던듯
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.web.multipart.MultipartFile;


  private String uploadFile(String type, MultipartFile multipartFile) throws Exception {
    String uploadDirStr = UPLOAD_DIR == null ? "/engn001/tomcat/8.5/servers/portal_8180/uploads" : UPLOAD_DIR;
    File uploadDir = new File(uploadDirStr);
    if (!uploadDir.exists() && !uploadDir.mkdirs())
      throw new Exception("uploadDir mkdir Fail!");
    File uploadFile = new File(uploadDirStr + "/" + multipartFile.getOriginalFilename());
    if (!uploadFile.getAbsolutePath().equals(uploadFile.getCanonicalPath()))
      throw new Exception("파일경로 및 파일명을 확인하십시오.");

    Path path = Paths.get(uploadDirStr + "/" + multipartFile.getOriginalFilename()).toAbsolutePath();
    multipartFile.transferTo(path.toFile());
    // multipartFile.transferTo(uploadFile);//file not found error
    String resultGftsUrl = uploadGfts(type, "KIC", uploadFile);
    uploadGfts(type, "AIC", uploadFile);
    uploadGfts(type, "EIC", uploadFile);
    uploadFile.delete();
    return resultGftsUrl.replaceFirst("http://kic-", "");
  }

 

반응형
반응형

[a tag]

<a href={questionModalSrc} download="88a2f203a144c233726b8ffcdccc9ed8.png">

href 주소의 Type이 이미지나 파일이어야만 다운로드가능,

  • download='img.png'에서 img.png는 변경가능
  • 확장자 안쓰면 알아서 다운로드시 Type에 해당하는 확장자 붙여줌,
  • img.htm이런식으로 확장자 잘못 붙여도 다운로드 가능

 

 <a href={`/board/viewimage?file=${encodeURIComponent(questionModalSrc)}`}>

Type이 document (html문서)라면 다운로드 에러남
href가 file Type이 아니면 이런식으로 다운로드 에러남, 이 때 img.htm의 확장자 htm은 저절로 알아서 붙은것

 

 

 

 

 

[모든 element에 적용가능한 방법] 클릭시 임시로 a tag생성하고 클릭되도록

onClick={() => { const link = document.createElement('a');  
                link.href = \`/common/files/01. CDP Admin Operation Policy.docx\`;  
                link.target = '\_blank';//링크클릭시 새창에서 열도록, 링크여는목적 아니므로 없어도 될듯  
                link.download = '다운로드될 파일명'; 이 부분이 없으면 자동다운로드 안됨
                document.body.appendChild(link);  
                link.click();  
                document.body.removeChild(link);  
              }}

 

 

 

 

 

외부에 있는 자원을 다운로드

API 구현부분

  • 테스트해보니 요청하는 헤더내의 content_type: application/json이지만 응답헤더의 Content-Type: application/octet-stream
    Content-Disposition: attachement; filename="RBSPOT.json"로 응답함
  • 참고로 다른 요청->응답은 아래와 같았음
    • 일반적인 json Fetch/XHR request -> response
      • content-type: application/json -> Content-Type: application/json;charset=UTF-8
    • 파일을 자동으로 다운로드되도록 하는 request -> response
      • content-type: application/json -> Content-Type: application/octet-stream
        Content-Disposition: attachement; filename="RBSPOT.json"
    • 파일을 서버로 업로드하는 request -> response
      • Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryCr6S7zpBWkWbkGAB ->
@Description("MetaDataFile Download from Admin.")
@GetMapping
@PreAuthorize("hasRole('ADMIN')")
@RequestMapping("/api/admin/apiCallLog/{apiCallLogId}/file")
public ResponseEntity<?> getMetaDataFile(@CurrentUser UserPrincipal currentUser,
    @PathVariable Long apiCallLogId) {

  Map<String, Object> resultMap = new HashMap<String, Object>();
  URL url = new URL(...어찌어찌해서 얻어옴);
  InputStreamResource resource = new InputStreamResource(url.openStream());
  resultMap.put("fileName", apiCallInfo.getFileName());
  resultMap.put("resource", resource);

  return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/octet-stream"))
      .header(HttpHeaders.CONTENT_DISPOSITION,
          "attachement; filename=\"" + resultMap.get("fileName") + "\"")
      .body(resultMap.get("resource"));
}

구현된 API call하는 부분

export async function getFileDownload(requestPath, requestMethod, rowData) {
  await fetch(API_BASE_URL + requestPath, {
    method: 'GET',
    headers: getHeaders(),
  })
    .then(response => {
      return response.blob();
    })
    .then(blob => {
      const url = window.URL.createObjectURL(
        new Blob([blob]),
      );
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute(
        'download',
        rowData.fileName,
      );//filename to be downloaded, it can be used as link.download=fileName
      //link.style='display:none'; //OK it is not given
      document.body.appendChild(link);
      link.click();
      link.parentNode.removeChild(link);
    })
    .catch(err => {
      alert('error')
    });
}

서버 내부 자원(public공간내 file) 다운로드

<button
                  type="button"
                  className="quick_link link1"
                  onClick={() => {
                    const link = document.createElement('a');
                    link.href = `/common/files/01. CDP Admin Operation Policy_(ENG)20210428.docx`;
                    link.target = '_blank';
                    link.download = '01. CDP Admin Operation Policy_(ENG)20210428.docx';
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                  }}>

서버응답을 받아 파일을 내려주거나 내부소스 파일을 내려주거나 다운로드 하는 부분의 공통점

const link = document.createElement('a');
link.href = `/common/files/01. CDP Admin Operation Policy_(ENG)20210428.docx`;//여기에서 차이남
//link.target = '_blank'; //없어도됨
link.download = '01. CDP Admin Operation Policy_(ENG)20210428.docx'; //link.setAttribute('download', 파일명)과 동일
document.body.appendChild(link);
link.click();
document.body.removeChild(link);//link.remove(); link.parentNode.removeChild(link);와 동일

서버응답을 받아 파일생성하는 부분

new Blob([arrayContetns], options) options는 생략가능

new Blob([response], {type" res.headers[content-type]})

Blob을 URL로 변환

window.URL.createObjectURL(blob)

URL.revokeObjectURL( window.URL.createObjectURL(blob) ) 여기서 window.URL=URL인듯

반응형
반응형
반응형
반응형
import useMediaQuery from '@material-ui/core/useMediaQuery';
const isMobile = useMediaQuery(theme.breakpoints.down('sm'));
반응형
반응형

클립보드 붙여넣기까지만 됨

  • [print screen]: 전체화면
  • [alt] + [print screen]: 선택한 창만
  • [window] + [shift] + [s]: 영역선택하면 복사됨

 

PrtScn 버튼만으로 화면캡처 크기를 조절하려면

[window]버튼클릭 > 설정버튼 클릭 > 접근성 > 상호작용 > 키보드 > [PrtScn 단추를 사용하여 화면 캡처 열기] 설정

 

붙여넣기 한 클립보드 확인

  • [window] + [v] 클릭하면 저장된 클립보드중 선택해서 사용가능

 

chrome debug tool 사용: 이미지가 저절로 저장됨

ctrl + shift + p : run command 나오면 capture로 search

  • capture area screenshot: 원하는 영역을 선택하면 자동다운로드됨
  • capture full size screenshot: scroll까지포함해서 전체다 자동다운로드됨
  • capture node screenshot: html문서에서 선택한 부분만 자동다운로드됨
  • capture screenshot: 딱 현재 화면만 자동다운로드됨

-> 단점은 브라우저 내부영역만 가능, 주소창(혹은 주소표시줄, addressBar), bookmarkBar등은 포함못함

반응형
반응형

an XML-based markup language for describing two-dimensional based vector graphics

반응형
반응형

 

https://marketing.istockphoto.com/blog/aspect-ratio/

 

What Are Common Aspect Ratios for Images?

Learn what at aspect ratio is, why it's important, and how to find the right aspect ratio for Facebook, Instagram, Snapchat, and YouTube, and other design projects.

marketing.istockphoto.com

 

Here are some common digital image sizes and their corresponding aspect ratios.

  • 1080 x 1080 pixels = 1:1 aspect ratio
  • 1080 x 720 pixels = 3:2 aspect ratio
  • 1024 x 768 pixels = 4:3 aspect ratio
  • 1920 x 1080 pixels = 16:9 aspect ratio
반응형
반응형

 

CSV는 결국 delimeter로 구분된 string

 

const Papa = require('papaparse/papaparse.min.js');

Papa.unparse(내용에 들어갈 array-[{a,b},{a2,b2},...]);

let blob = new Blob(['\ufeff' + bodyData]), url = window.URL.createObjectURL(blob);
let a = document.createElement('a');
document.body.appendChild(a);
a.style = 'display: none';
a.href = url;
a.download = 'exportEventLogs.csv';
a.click();
window.URL.revokeObjectURL(url);//revokeObjectURL: URL.createObjectURL()로 생성한 객체 URL해제
//참고로 revoke는 re + voke(vocal 과 같은 어원, 부르다) 다시 불러들이다 -> 취소/해제하다

let blob = new Blob(['\ufeff' + csvData]),

여기서 \ufeff 붙이는 이유는? 

  • /ufeff는 해당 file이 어떤 문자열로 코딩되었는지를 의미하는 일종의 식별자입니다. “이 문서의 인코딩 방식은 뭐에요”라는 걸 알려주는 메타 정보인 것이죠. 보통 이러한 방식은 해당 파일의 맨 앞에 집어넣곤 합니다.
  • --> 이런걸 unicode BOM(Byte Order Mark)라고 하나봄

https://frhyme.github.io/python-basic/py_eff_byte_order_mark/

 

python에서 \ufeff가 읽힐 때 해결방법.

1-line summary

frhyme.github.io

 

BOM이란 문서 맨 앞에 눈에 보이지 않는 특정 바이트(byte)를 넣은 다음 이것을 해석해서 정확히 어떤 인코딩 방식이 사용되었는지 알아내는 방법을 나타냅니다. 자세하게 유니코드가 little-endian 인지 big-endian 인지 아니면 UTF-8 인지 쉽게 알 수 있도록, 유니코드 파일이 시작되는 첫부분에 보이지 않게, 2~3바이트의 문자열을 추가하는데 이것을 BOM이라고 합니다. BOM은 텍스트 에디터 화면에서는 보이지 않고, 헥사 에디터(Hex Editor)*로 열었을 때만 보입니다.

https://brownbears.tistory.com/124

 

유니코드 BOM(Byte Order Mark)

BOM이란 BOM이란 문서 맨 앞에 눈에 보이지 않는 특정 바이트(byte)를 넣은 다음 이것을 해석해서 정확히 어떤 인코딩 방식이 사용되었는지 알아내는 방법을 나타냅니다. 자세하게 유니코드가 litt

brownbears.tistory.com

 

 


function extractBodyFieldKey(body, timeType, intl) {
 let addUpDay = body.reduce( (acc, cur) => {
  return acc.concat(cur);
 });
 addUpDay = sortByTimestamp(addUpDay);
 return Papa.unparse(addUpDay.map(log => {
  let eventTime = getTimeZoneTime(log, timeType);
  let event =  intl.formatMessage({id:  util.eventLogMessageByCode(log.code)});
  let userId = log.userKey;
  let userName = log.userName;
  let deviceDisplayName = log.deviceName;
  return {eventTime, event, userId, userName, deviceDisplayName}
 }))
}

[07-22 오전 11:30] 구본관: 저희 모카키도 그렇고 에어팝 포털도 그렇고 CSV 내보내기 할 때 대량으로 생성하게 되면 block 걸리는 경우가 발생합니다. 위에 이번에 변경된 UI도 파일작업을 할 때 동작을 멈추게 됩니다. addUpDay.map 이나 Papa.unparse 부분에서 시간이 굉장히 오래 걸리게 되는데 이때 렌더링이 멈추게 됩니다 대량으로 데이터를 처리할때는 비동기 방식으로 처리할 필요가 있습니다. 저희가 사용하는 papaparse 라이브러리를 보면 step 이나 chunk 같은 옵션들이 있습니다. 이번에 수정하려고 하는 것은 아니고 추후 개발에 참고하시면 좋겠습니다

-> 시간이 오래 걸려서 렌더링 멈춤? 상관없고 대량 데이터를 array로 한번에 처리해서 outOfMemory날뿐, 잘라서 처리해주면됨

 

반응형

'js' 카테고리의 다른 글

regular expression  (2) 2023.11.08
변수의 true/false를 이용한 react 조건부 rendering (0 error case)  (0) 2022.08.31
개발환경에 따른 console.log 분리  (0) 2022.08.17
js array 중복제거  (0) 2022.08.08
moment  (0) 2022.07.25
반응형

.env.[localhost|development|production] 파일로 분리되어 각각의 파일안에 정의하고

REACT_APP_SERVER_LOCATION="DEV"

package.json에서 --config webpack.[localhost|development|production].js 를 설정하면

{
  "name": "asc_portal",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "start:webapp:local": "curl -X POST localhost:8090/language/info > src/language.json | npx --max_old_space_size=4096 webpack serve --mode development --env PRODUCT=portal TITLE=\"# LOCAL\" --config webpack.localhost.js",
    "build:webapp:development": "curl -X POST https://csportaltest.lge.com/api/language/info > src/language.json | npx webpack --mode development --env PRODUCT=portal TITLE=\"# DEV\" --config webpack.development.js",
    "build:webapp:production": "curl -X POST https://csportaltest.lge.com/api/language/info > src/language.json | npx webpack --mode production --env PRODUCT=portal TITLE=\"LG CS Portal\" --config webpack.production.js",
    "start": "react-scripts start",
    "build": "npx webpack --mode production --env PRODUCT=portal TITLE=\"LG CS Portal\" --config webpack.production.js",
    "build:dev": "env-cmd -f .env.test react-scripts build",
    "build:2nd": "env-cmd -f .env.test.2nd react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

.env.[개발환경]안에 정의한 변수명 process.env.REACT_APP_SERVER_LOCATION 으로 사용가능


[mocaKey] package.json안에서 set REACT_APP_VERSION=dev 지정하면 process.env.REACT_APP_VERSION변수로 사용

"scripts": {
  "start": "set HTTPS=true&&react-scripts start",
  "start:beta": "set HTTPS=true&&set REACT_APP_VERSION=beta&&react-scripts start",
  "start:dev": "set HTTPS=true&&set REACT_APP_VERSION=dev&&react-scripts start",
  "hot": "react-app-rewired start",
  "build": "react-scripts build",
  "build:beta": "set REACT_APP_VERSION=beta&&react-scripts build",
  "build:dev": "set REACT_APP_VERSION=dev&&react-scripts build",
  "test": "react-scripts test --watchAll=false",
  "test:verbose": "react-scripts test --verbose",
  "test:report": "react-scripts test --verbose 2> report.txt",
  "test:coverage": "react-scripts test --coverage --watchAll=false",
  "eject": "react-scripts eject",
  "cypress:open": "cypress open",
  "cypress": "cypress run --browser=chrome",
  "cypress:test": "set START_SERVER_AND_TEST_INSECURE=1&&start-server-and-test start:dev https-get://localhost:3000 cypress"
},
let LEVEL = process.env.REACT_APP_VERSION;
LEVEL = LEVEL || dev;

if (LEVEL !== dev) {
  console.log = function() {};
  console.warn = function() {};
  console.debug = function() {};
}

 

반응형

+ Recent posts